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'.