How to convert (0,0) to [0,0] in prologue?
While the others have answered, keep in mind that
Prolog is actually not what you might imagine:
?- write_canonical((a,b)). ','(a,b) true.
So this is the term
. If you are working with pairs, you can do two things that are probably "prettier":
Keep them as a "couple"
?- write_canonical(a-b). -(a,b) true.
The advantage is that pairs like this can manipulate a bunch of de facto standard predicates, for example
as well as library (pairs) .
Or, if they are a data structure that is part of your program, you can also make it explicit, for example in
. The distance in two-dimensional space will then take on two terms
distance(coor(X1, Y1), coor(X2, Y2), D) :- D is sqrt((X1-X2)^2 + (Y1-Y2)^2).
If you don't know how many measurements you have, you can store the coordinates of each point in the list. The message here is that lists are for things that may have 0 or more elements in them, while pairs or other terms with arity 2 or any term with known arity are clearer about the number of elements they have.
If you just have a simple pair, you can use an operator
and just say something like:
X = (a,b) , X =.. [_|Y] .
X = (a,b) . Y = [a,b] .
It doesn't work if
- it's something like
creating how it does
X = (a,b,c) . Y = [a,(b,c)] .
[maybe not what you want].
The more general case is pretty simple:
csv2list( X , [X] ) :- % We have a list of length 1 var(X) . % - if X is UNbound csv2list( X , [X] ) :- % We have a list of length 1 nonvar(X) , % - if X is bound, and X \= (_,_) . % - X is not a (_,_) term. cs22list( Xs , [A|Ys] ) :- % otherwise (the general case) , nonvar(Xs) , % - if X is bound, and Xs = (A,Bs) , % - X is a (_,) term, csv2list(Bs,Ys % - recurse down added the first item to result list. . % Easy!
source to share