比较序言中两个列表的内容

发布于 2024-12-21 18:37:24 字数 466 浏览 5 评论 0原文

我正在做一些家庭作业,但我被困在一个点上。我得到了一些这样的事实:

word([h,e,l,lo]).
word([m,a,n]).
word([w,o,m,a,n]). etc

我必须制定一条规则,以便用户输入一个字母列表,我应该将该列表与我拥有的单词进行比较并纠正任何可能的错误。如果第一个字母位于正确的位置,这是我正在使用的代码:

mistake_letter([],[]).
mistake_letter([X|L1],[X|L2]):-
             word([X|_]),
             mistake_letter(L1,L2).

问题是我不知道如何移至事实一词中的下一个字母。下次回溯运行时,它将使用单词的头部,而我想使用列表中的第二个字母。关于如何解决这个问题有什么想法吗?

对于任何语法错误,我深表歉意,并感谢您的帮助。

I am having some kind of homework and I am stuck to one point. I am given some facts like those:

word([h,e,l,lo]).
word([m,a,n]).
word([w,o,m,a,n]). etc

and I have to make a rule so that the user will input one list of letters and I should compare the list with the words I have and correct any possible mistakes. Here is the code I am using if the first letter is in the correct place:

mistake_letter([],[]).
mistake_letter([X|L1],[X|L2]):-
             word([X|_]),
             mistake_letter(L1,L2).

The problem is I don't know how to move to the next letter in the word fact. The next time the backtrack will run it will use the head of the word while I would like to use the second letter in the list. Any ideas on how to solve this?

I am sorry for any grammatical mistakes and I appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2024-12-28 18:37:24

为了移动到事实单词中的下一个字母,您需要将事实中的单词作为第三个参数,并随身携带。在 mistake_letter/2 中,您将逐个选择单词,然后调用 mistake_letter/3,传递您选择的单词,如下所示

mistake_letter(L1,L2):-
    word(W),
    mistake_letter(L1,L2,W).

:当被纠正的单词中的字母在您选择的单词的字母之前用完时,更改您的基本情况以执行某些操作。您要做什么取决于您的作业:您可以回溯 mistake_letter([],[],[]).,声明匹配 mistake_letter([],[],_). code>,将单词的尾部附加到更正错误 mistake_letter([],W,W). 或执行其他操作。

您还需要一个简单的案例来涵盖当被更正的单词的第一个字母与您选择的单词的第一个字母匹配时的情况:

mistake_letter([X|L1],[X|L2],[X|WT]):-
    mistake_letter(L1, L2, WT).

最后,您需要最重要的案例:当首字母不匹配时该怎么办。这可能是您作业的大部分内容:其余的只是样板递归。为了得到正确的结果,您可能需要将 mistake_letter/3 更改为 mistake_letter/4 才能计算匹配数,然后将其与数字进行比较原始单词中的字母。这可以让你删除“更正”,例如 [w,o,r,l,d] --> [h,e,l,l,o] 仅包含 20% 的匹配字母。

In order to move to the next letter in the word fact, you need to make the word from the fact a third argument, and take it along for the ride. In your mistake_letter/2, you will pick words one by one, and call mistake_letter/3, passing the word you picked along, like this:

mistake_letter(L1,L2):-
    word(W),
    mistake_letter(L1,L2,W).

The you'll need to change your base case to do something when the letters in the word being corrected run out before the letters of the word that you picked. What you do depends on your assignment: you could backtrack mistake_letter([],[],[])., declare a match mistake_letter([],[],_)., attach word's tail to the correction mistake_letter([],W,W). or do something else.

You also need an easy case to cover the situation when the first letter of the word being corrected matches the first letter of the word that you picked:

mistake_letter([X|L1],[X|L2],[X|WT]):-
    mistake_letter(L1, L2, WT).

Finally, you need the most important case: what to do when the initial letters do not match. This is probably the bulk of your assignment: the rest is just boilerplate recursion. In order to get it right, you may need to change mistake_letter/3 to mistake_letter/4 to be able to calculate the number of matches, and later compare it to the number of letters in the original word. This would let you drop "corrections" like [w,o,r,l,d] --> [h,e,l,l,o] as having only 20% of matching letters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文