I'm looking to convert a Prolog term similar to this:
cons(a, cons(b, cons(c, cons(d, nil ))))
into a list:
[a, b, c, d]
I have a function to verify that cons(a, cons(b, cons(c, cons(d, nil ))))
is a proper list, as follows:
list(nil).
list(cons(_,X)):-
list(X).
which yields,
?- list(cons(a, cons(b, cons(c, cons(d, nil ))))).
true.
Now, when I pass a term like, list(cons(a, cons(b, cons(c, cons(d, nil )))))., and it's true, I want to then be able to convert it to a list. I can do all of the rest, I'm just stuck on the conversion from the term to a list.
Any assistance pointing me in the right direction would be greatly appreciated.
'.'(H, T)whereHis the head andTis the tail (or rest) of the list. The bracket notation[...]is just a syntactic "sugar". So'.'is actually Prolog's version ofconssince the real form of[a,b,c,d]is'.'(a, '.'(b, '.'(c, '.'(d, [])))). - lurker