Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prolog Help. Help would be greatly appreciated

Status
Not open for further replies.

Joey123456

Programmer
Jan 4, 2007
1
MT
Hey guys i have some prolog work for a class im taking and it must be handed in in 3 days. I was wondering if someone could please work them out for me. Theyre not hard for pros. Please, many thanks to whoever helps me out :):)

1.Write clauses for the following list handling rules:

a.insert(Element,OldList,NewList) which inserts Element in the OldList to produce the NewList.
e.g. insert(8,[1,2,3],NL) binds NL to [8,1,2,3]

b.right(List,Element) which binds Element to the rightmost member within the List.
e.g. right([charlie, joey, malcolm, mary],R) binds R to ‘mary’

3.Write recursive clauses for the following predicates:

3a.deduct(List_of_Integers, Number, Result) which binds Result to the value of the Number after that each element within the List_of_Integers are deducted from it.

3b. display_items(List_of_Items) which displays a sequence of the items within the List_of_Items from left to right.

3c.push(Item, List, Stack) which binds Stack to List with the Item entered at its head.
 
for a.
insert(Element,OldList,NewList):-NewList = [Element|OldList].
 
for b.

right([X],El):-El=X.

right([_|T],El):-right(T,El).
 
for 3b.

display_items([]).
display_items([H|T]):put(H),display_items(T).

though it produces the elements in the list,it leaves no spaces and also produces a "Yes" in the end.

if it doesnt suit you, try to alter it yourself to meet with your needs.
 
for 3a and 3c i do not understand the explanation you give.
if you could specify it more maybe i could do something about it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top