关于儿童的逻辑任务
一个家庭有5个孩子。他们的年龄:4、5、6、7和8岁。他们的名字:Rose,Becca,Iona,Stu,Rob。他们每个人都有才华:钢琴,数学,小提琴,文学,计算机。 如果已知以下事实,我需要找出每个孩子的年龄和才华:
- Becca已有4岁,不知道数学。
- 一个孩子,非常了解计算机比Stu
- 大一年龄的孩子一岁的孩子扮演小提琴艾奥娜
- Rob )不是8岁
- ,他年仅5岁
( 不起作用,因为我使用swi-prolog,并且还有其他一些语法:
domains
ID= symbol
Age = h(ID,NO)
HLIST = reference Age*
NO = integer
NOLIST = NO*
predicates
nondeterm solve
nondeterm candidate(HLIST,HLIST)
nondeterm perm(HLIST)
nondeterm permutation(NOLIST,NOLIST)
nondeterm delete(NO,NOLIST,NOLIST)
member(Age,HLIST)
clauses
solve():- candidate(Talents,Names),
member(h(Becca,4),Names),member(h(maths,mathsA),Talents),mathsA<>4,
member(h(Stu,StuA),Names),member(h(computer,compA),Talents),compA=StuA+1,
member(h(violin,7),Talents),member(h(Iona,IonaA),Names),IonaA<>8,
member(h(Rob,5),Names),member(h(literature,literatureA),Talents),literatureA>5,
member(h(piano,_pianoA),Talents),member(h(Rose,RoseA),Names),
member(h(BeccaT,4),Talents),member(h(StuT,StuA),Talents),
member(h(IonaT,IonaA),Talents),member(h(RobT,5),Talents),
member(h(RoseT,RoseA),Talents),write("Becca - 4 - ",BeccaT),nl,
write("Stu - ",StuA," - ",StuT),nl,
write("Iona - ",IonaA," - ",IonaT),nl,
write("Rob - 5 - ",RobT),nl,
write("Rose - ",RoseA," - ",RoseT),nl.
candidate(L1,L2):- perm(L1),perm(L2).
perm([h(_,A),h(_,B),h(_,C),h(_,D),h(_,E)]):- permutation([A,B,C,D,E],[4,5,6,7,8]).
permutation([],[]).
permutation([A|X],Y):-delete(A,Y,Y1),permutation(X,Y1).
delete(A,[A|X],X).
delete(A,[B|X],[B|Y]):-delete(A,X,Y).
member(A,[A|_]):-!.
member(A,[_|X]):-member(A,X).
goal solve.
我需要为swi-prolog。
结果。我在哪里犯了错误?
puzzle(Children) :-
Children = [child('Becca', 4, _),
child('Rob', 5, _),
child(_, 6, _),
child(_, 7, 'Violin'),
child(_, 8, _)],
member(child('Rose', _, _), Children),
member(child(_, _, 'Piano'), Children),
member(child('Becca', AgeB, TalentB), Children),
member(child(_, AgeM, 'Mathematics'), Children),
member(child('Stu', AgeS, TalentS), Children),
member(child(_, AgeC, 'Computer'), Children),
member(child('Iona', AgeI, _), Children),
member(child('Rob', AgeR, TalentR), Children),
member(child(_, AgeL, 'Literature'), Children),
TalentB \= 'Mathematics',
AgeM > AgeB,
AgeS + 1 = AgeC,
AgeS < 8,
TalentS \= 'Computer',
AgeI < 8,
AgeL > AgeR,
TalentR \= 'Literature'.
A family has 5 children. Their ages: 4, 5, 6, 7 and 8 years old. Their names: Rose, Becca, Iona, Stu, Rob. Each of them has a talent: piano, math, violin, literature, computer.
I need to find out each child's age and talent, if the following facts are known:
- Becca is 4 years old and doesn't know math.
- A child, who know computer very well is one year older than Stu
- A 7 years old child plays violin
- Iona is not 8 years old
- Rob is 5 years old and he's younger then a child whose talent is literature
I have an example solution, but it doesn't work, because I use Swi-prolog and it has some other syntax:
domains
ID= symbol
Age = h(ID,NO)
HLIST = reference Age*
NO = integer
NOLIST = NO*
predicates
nondeterm solve
nondeterm candidate(HLIST,HLIST)
nondeterm perm(HLIST)
nondeterm permutation(NOLIST,NOLIST)
nondeterm delete(NO,NOLIST,NOLIST)
member(Age,HLIST)
clauses
solve():- candidate(Talents,Names),
member(h(Becca,4),Names),member(h(maths,mathsA),Talents),mathsA<>4,
member(h(Stu,StuA),Names),member(h(computer,compA),Talents),compA=StuA+1,
member(h(violin,7),Talents),member(h(Iona,IonaA),Names),IonaA<>8,
member(h(Rob,5),Names),member(h(literature,literatureA),Talents),literatureA>5,
member(h(piano,_pianoA),Talents),member(h(Rose,RoseA),Names),
member(h(BeccaT,4),Talents),member(h(StuT,StuA),Talents),
member(h(IonaT,IonaA),Talents),member(h(RobT,5),Talents),
member(h(RoseT,RoseA),Talents),write("Becca - 4 - ",BeccaT),nl,
write("Stu - ",StuA," - ",StuT),nl,
write("Iona - ",IonaA," - ",IonaT),nl,
write("Rob - 5 - ",RobT),nl,
write("Rose - ",RoseA," - ",RoseT),nl.
candidate(L1,L2):- perm(L1),perm(L2).
perm([h(_,A),h(_,B),h(_,C),h(_,D),h(_,E)]):- permutation([A,B,C,D,E],[4,5,6,7,8]).
permutation([],[]).
permutation([A|X],Y):-delete(A,Y,Y1),permutation(X,Y1).
delete(A,[A|X],X).
delete(A,[B|X],[B|Y]):-delete(A,X,Y).
member(A,[A|_]):-!.
member(A,[_|X]):-member(A,X).
goal solve.
I need to write this or other solution for Swi-Prolog.
I wrote a code, but I got the "false" result. Where did I make mistakes?
puzzle(Children) :-
Children = [child('Becca', 4, _),
child('Rob', 5, _),
child(_, 6, _),
child(_, 7, 'Violin'),
child(_, 8, _)],
member(child('Rose', _, _), Children),
member(child(_, _, 'Piano'), Children),
member(child('Becca', AgeB, TalentB), Children),
member(child(_, AgeM, 'Mathematics'), Children),
member(child('Stu', AgeS, TalentS), Children),
member(child(_, AgeC, 'Computer'), Children),
member(child('Iona', AgeI, _), Children),
member(child('Rob', AgeR, TalentR), Children),
member(child(_, AgeL, 'Literature'), Children),
TalentB \= 'Mathematics',
AgeM > AgeB,
AgeS + 1 = AgeC,
AgeS < 8,
TalentS \= 'Computer',
AgeI < 8,
AgeL > AgeR,
TalentR \= 'Literature'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论