Anthony Gauthier
Anthony Gauthier

Reputation: 21

Making a list from a tuple

I am trying to make a list from a tuple of variable size. However, I am having trouble figuring out how to represent an empty tuple (or a tuple with a single value in it), which I need as my end case.

This is what I have right now which, judging by the trace, does create a list (reversed however but it's not really a problem) but it fails at the very end.

tuple_to_list((), []).
tuple_to_list((X, ()), [X]).
tuple_to_list((X, XS), List) :-
    tuple_to_list(XS, [X|List]).

Upvotes: 0

Views: 63

Answers (1)

joel76
joel76

Reputation: 5645

Just :

tuple_to_list((X, XS),[X | List]) :-
    tuple_to_list(XS, List).
tuple_to_list((X), [X]):-
    X \= (_,_).

Last clause X \= (,). because of

?- (X) = (a,b).
X =  (a, b).

Upvotes: 1

Related Questions