序言中的新手:Simple Solve [英] Newbie in prolog: Simple Solve

查看:46
本文介绍了序言中的新手:Simple Solve的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 prolog 的新手,我正在尝试解决这个问题:

I am new to prolog, and I am trying to solve this:

它只是返回 false!

It just returns false!

我曾多次尝试更改代码的几个部分,因为我说我是 prolog 的新手,因此我不知道如何准确调试..

I have tried changing several parts of the code several times, as I said I'm a newbie in prolog and thus I don't know how to debug exactly..

livesIn(State):-
member(State,[california,georgia,delaware,iowa,kansas]).

sentGift(Gift):-
member(Gift,[rotisserie,salver,toaster,urn,vase]).

isHusband(Husband):-
member(Husband,[bill,doug,nick,tom,zack]).

solve(Z):-
Z=[[amber,Husband1,State1,Gift1],
   [emily,Husband2,State2,Gift2],
   [janet,Husband3,State3,Gift3],
   [maisie,Husband4,State4,Gift4],
   [patsy,Husband5,State5,Gift5]],

isHusband(Husband1), isHusband(Husband2), isHusband(Husband3), isHusband(Husband4), isHusband(Husband5),
Husband1 \== Husband2, Husband1 \== Husband3, Husband1 \== Husband4, Husband1 \== Husband5,
Husband2 \== Husband1, Husband2 \== Husband3, Husband2 \== Husband4, Husband2 \== Husband5,
Husband3 \== Husband1, Husband3 \== Husband2, Husband3 \== Husband4, Husband3 \== Husband5,
Husband4 \== Husband1, Husband4 \== Husband2, Husband4 \== Husband3, Husband4 \== Husband5,
Husband5 \== Husband1, Husband5 \== Husband2, Husband5 \== Husband3, Husband5 \== Husband4,

livesIn(State1), livesIn(State2), livesIn(State3), livesIn(State4), livesIn(State5),
State1 \== State2, State1 \== State3, State1 \== State4, State1 \== State5,
State2 \== State1, State2 \== State3, State2 \== State4, State2 \== State5,
State3 \== State1, State3 \== State2, State3 \== State4, State3 \== State5,
State4 \== State1, State4 \== State2, State4 \== State3, State4 \== State5,
State5 \== State1, State5 \== State2, State5 \== State3, State5 \== State4,

sentGift(Gift1), livesIn(Gift2), livesIn(Gift3), livesIn(Gift4), livesIn(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,
Gift2 \== Gift1, Gift2 \== Gift3, Gift2 \== Gift4, Gift2 \== Gift5,
Gift3 \== Gift1, Gift3 \== Gift2, Gift3 \== Gift4, Gift3 \== Gift5,
Gift4 \== Gift1, Gift4 \== Gift2, Gift4 \== Gift3, Gift4 \== Gift5,
Gift5 \== Gift1, Gift5 \== Gift2, Gift5 \== Gift3, Gift5 \== Gift4,

%Aunt Maisie's Husband is Uncle Nick
Husband4 = nick,

%Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in order)
Gift2 = salver,
member([_, bill, _, urn], Z),

%Aunt Amber and Uncle Doug live in Georgia
Husband1 = doug,
State1 = georgia,

%Aunt Patsy and her husband sent a toaster
Gift5 = toaster,

%The rotisserie came from the Delaware relatives
member([_, _, delaware, rotisserie], Z),

%Uncle Zack and his wife(don't live in Kansas) sent neither the salver nor the toaster
\+ member([_, zack, kansas, _], Z),
\+ member([_, zack, _, salver], Z),
\+ member([_, zack, _, toaster], Z),

%Aunt Janet and Uncle Bill live in California
Husband3 = bill,
State3 = california.

推荐答案

使其有效

您的代码中有 2 个拼写错误,无法返回正确的结果.

Make it works

There are 2 typos in your code that prevents it from returning the correct result.

第一个错别字

比尔叔叔和艾米丽婶婶以及他们各自的配偶送了托盘和瓮(按顺序)

Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in order)

所以代替:

Gift2 = salver,
member([_, bill, _, urn], Z),

应该是:

Gift2 = urn,                       % Aunt Emily sent the urn
member([_, bill, _, salver], Z),   % Uncle Bill sent the salver

第二个错别字

我不知道你是怎么打出这个错字的!为什么 livesIn 礼物?

I don't know how you can make this typo! Why livesIn a gift?

sentGift(Gift1), livesIn(Gift2), livesIn(Gift3), livesIn(Gift4), livesIn(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,

应该是:

sentGift(Gift1), sentGift(Gift2), sentGift(Gift3), sentGift(Gift4), sentGift(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,

修正上述 2 个错别字后,代码将正常工作,但需要很长时间才能找到解决方案.

After fixing the 2 typos above, the code will work correctly, but it will take very long time to reach the solution.

  1. 命令与原子的直接统一(例如 Husband3 = bill, State1 = georgia)之前GiftHusbandState 变量通过 sentGiftisHusbandlivesIn 谓词.

  1. Order the direct unification with an atom (e.g. Husband3 = bill, State1 = georgia) before the membership unification of the Gift, Husband and State variables via sentGift, isHusband and livesIn predicates.

实际上,您可以将它们直接插入Z.而不是手动声明所有这些不等式(例如 Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5),而是写一个谓词更干净.

Actually, you can just plug them directly to Z. And instead of declaring all those inequality manually (e.g. Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5), it is cleaner to write a predicate instead.

在第 1 部分中的直接统一之后但仍然在 <的成员统一之前订购成员统一(例如 member([_, bill, _, urn], Z))code>Gift、HusbandState 变量.

Order the membership unification (e.g. member([_, bill, _, urn], Z)) after the direct unification in part 1, but still before membership unification of the Gift, Husband and State variables.

请注意,这包括\+ member([_, zack, _, toaster], Z),它断言成员资格统一不能 成功.在将 GiftHusbandState 变量限制为一小组可能值的成员资格统一之前,可能有可能找到满足成员合一的合一,导致子句和整个谓词失败.

Note that this does not include \+ member([_, zack, _, toaster], Z), which asserts that membership unification CANNOT succeed. Before the membership unification of the Gift, Husband and State variables which constraint them to a small set of possible values, it might be possible to find a unification that satisfy the membership unification, which leads to the clause and thus the whole predicate failing.

上述 2 个技巧将减少搜索空间,从而减少找到解决方案所需的时间.下面是一些示例骨架代码,供您在阅读以上文本后填写.

The 2 tricks above will reduce the search space and therefore reduce the time needed to find a solution. Below is some sample skeleton code for you to fill in when you have read the text above.

% Aunt Maisie's Husband is Uncle Nick
Husband4 = nick,

%% TODO: Fill in the rest on your own ...

% Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in order)
Gift2 = urn,




member([_, bill, _, salver], Z),

% The rotisserie came from the Delaware relatives
member([_, _, delaware, rotisserie], Z),




sentGift(Gift1), sentGift(Gift2), sentGift(Gift3), sentGift(Gift4), sentGift(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,
Gift2 \== Gift1, Gift2 \== Gift3, Gift2 \== Gift4, Gift2 \== Gift5,

%% TODO: Fill in the rest on your own





% Uncle Zack and his wife(don't live in Kansas) sent neither the salver nor the toaster
\+ member([_, zack, kansas, _], Z),
\+ member([_, zack, _, salver], Z),
\+ member([_, zack, _, toaster], Z).

这篇关于序言中的新手:Simple Solve的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆