Sicstus Prolog - 将单词放入列表中
还有一个问题: 有一个包含一些单词的列表。如果单词的长度超过给定长度(例如 4),它将被放入另一个列表中。
我尝试过:
require_min_length([], _, []).
require_min_length([Word|Words], Minl, [List]):-
word_length(Word, W), % method word_length return the length of a word.
(W >= Minl -> append(Word, List, List), require_min_length(Words, Minl, List);
require_min_length(Words, Minl, List)).
我得到的结果:
| ?- Words=["ABCD", "ABCDE", "AAA"], require_min_weight(Words, 5, Lists).
! Resource error: insufficient memory.
正确的结果将是:
Lists = [[65, 66, 67, 68, 69]]. (% ascii)
如何更改代码?请任何帮助!谢谢。
Got another problem:
There is a list with some words. If the length of the word is more than a given length (ex. 4), it will be put in an another list.
I tried:
require_min_length([], _, []).
require_min_length([Word|Words], Minl, [List]):-
word_length(Word, W), % method word_length return the length of a word.
(W >= Minl -> append(Word, List, List), require_min_length(Words, Minl, List);
require_min_length(Words, Minl, List)).
Results I got:
| ?- Words=["ABCD", "ABCDE", "AAA"], require_min_weight(Words, 5, Lists).
! Resource error: insufficient memory.
The right result will be:
Lists = [[65, 66, 67, 68, 69]]. (% ascii)
How to change the code? Any help pls! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的谓词的问题是您使用了错误的附加。
您正在使用append(Word, List, List),只有当Word是空列表时才会成功。另外,您确实不想将单词的代码附加到输出列表中,而是附加单词本身。
考虑这样的事情:
The problem with your predicate is that you are using append wrong.
You are using append(Word, List, List), this would only succeed if Word is an empty list. Also you really don't want to append the codes of your word to the output list, but the word itself.
Consider something like this: