So if I have something like [(a,b,c,d)]
and I wish to remove the ()s (in开发者_JAVA技巧 the whole list there will only be one set of parentheses - immediately after the '[' and before the ']'), how come my rule: curly_for_square( [(C)], [C] ).
doesn't work? Seems like it should but I'm sure there's a simple reason!!
I'm not entirely sure exactly what [(a,b,c,d)]
is, but I suspect it's equivalent to [ ','(a, ','(b, ','(c, d))) ]
, using the infix comma operator three times, since there's no functor taking those four arguments.
If you absolutely must use that syntax, maybe something like this would help convert to an ordinary list:
decomma( List, [Head | TailOut] ) :- List = [','(Head,TailIn)], !,
decomma( [TailIn], TailOut ).
decomma( [Term], [Term] ).
To convert back and forth between [a,b,c, ...] and (...(((a,b),c) ... )
flatten(X, H, T) :- var(X), !, H = [X|T]. flatten((A,B), H, T) :- !, flatten(A, H, X), flatten(B, X, T). flatten(X, [X|T], T). unflatten([X], X) :- !. unflatten([A,B|Cs], F) :- unflatten([(A,B)|Cs], F).
精彩评论