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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reverse and intersect problems

Status
Not open for further replies.

programmer720

Programmer
Joined
Nov 21, 2002
Messages
2
Location
US
I'm looking for these 2 algorithms...
REVERSE: given a list L, find the reverse R of L (top-level elements only). For example,
?-reverse([ ], R). should answer R = [ ]
?-reverse([1, 2, [3, 4]], R). should answer R = [[3, 4], 2, 1].

INTERSECT: given sets X and Y (represented as lists), intersect(X, Y, I) finds I which is X¿Y.

Thanks a lot.
 
i don't know about INTERSECT at the moment... but for REVERSE you can have:

reverse(L, Reversed) :-
reverse1(L, [], Reversed).
reverse1([], R, R).
reverse1([H|ToGo], Sofar, Reversed) :-
reverse1(ToGo, [H|Sofar], Reversed).

smiletiniest.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top