Prolog Ancestor with List -


i searched around , couldn't find answer. i'm having trouble making genealogy list.

so, have is_a relations, example:

is_a(cow, animal). is_a(calf, cow). is_a(dog, animal). .... etc. 

i want have procedure following:

toanimal(cow, x).  outputs x= [calf, cow, animal]. 

basically, if give input(cow), go cow until animal , add every step list.

so far, have this:

toanimal(a, b) :- is_a(a,b). toanimal(a, b) :- is_a(a, x), toanimal(x, b). 

the output of be

x= cow; x = animal; false 

how become list?

edit:

descend(x,y) :- is_a(x,y). descend(x,y) :- is_a(x,z), descend(z,y). toanimal(x,y):- findall(x, descend('animal', x), y). 

i have updated after looking @ suggestion. however, how list print? i'm still new prolog. findall page said return list, not doing me.

toanimal(calf, y) outputs: false. 

edit:

it returns empty list. i'm not sure issue here. have not changed code @ all, output should not change has.

edit:

thanks mrbratch response. made suggested changes, have issue. example, if have relations:

is_a(calf, cow). is_a(calf, animal). is_a(cow, cool). is_a(cool, awesome). 

but want path calf awesome. code give me possible paths calf,x. example,

descend(x,y) :- is_a(x,y). descend(x,y) :- is_a(x,z), descend(z,y). toawesome(a,y) :-     findall(x, descend(calf, x), y). 

will give me list y has

[cow,animal,cool,awesome]. 

but want

[calf,cow,cool,awesome]. 

how filter other paths? , add starting point? suppose can append calf beginning head, how ignore other paths?

edit:

thanks figured out, lose end path , start path. example, l contains cow,cool. calf , awesome not there. tried appending don't understand syntax. i'm not allowed append(x,l,anewlist)?

descend(x,y) :- is_a(x,y). descend(x,y) :- is_a(x,z), descend(z,y). toanimal(a,b) :-     setof(x, (descend(a,x), descend(x,'awesome')), b).  -->   append(a, l,anewlist).  ??    line not allowed here? how else it? or there simpler way add beginning 

this sample more or less want:

is_a(cow, animal). is_a(calf, cow). is_a(dog, animal). is_a(snoopy, dog). is_a(lassie, collie). is_a(collie, dog).  toanimal3( x, [x,animal] , animal ):- is_a( x, animal). toanimal3( x, [x|r], r ):- is_a( x, y), toanimal3(y, r, _).  :- initialization(main). main :- toanimal3( lassie, a, b), write(a), write(b). 

when run, output:

[lassie,collie,dog,animal][collie,dog,animal]

tested online using this prolog online interpreter

post edit: ah, it! should've written "[x,animal]" instead of "[x|animal]" first clause! galore @mbratch , program intended.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -