1

I have a predicate that decomposes an expression and I am having results that I dont understand when I trace through it.

My predicate is as below

 calc(R,Expr) :- Expr =..[Op,H,T].

So when I have an expression like [1,1], the Op is actually a period. Any idea why?

kkh
  • 4,799
  • 13
  • 45
  • 73
  • A [related question](http://stackoverflow.com/questions/10022779/prolog-unusual-cons-syntax-for-lists/) you might be interested in. – false Oct 23 '12 at 23:03

1 Answers1

4

It's because the form [Head|Tail] is just syntactic sugar for '.'(Head, Tail) (everything is a term and lists are no exception). Normally, for the list made of 1, 2, 3 and 4, you'd have to write

'.'(1, '.'(2, '.'(3, '.'(4, [])))).

As you can see, that's not very practical and instead we use the shortcut:

[1|[2|[3|[4|[]]]]]

And this shortcut has a shortcut:

[1, 2, 3, 4]

And you can mix those as you wish:

[1, 2|[3, 4]]

That is handy when you want to specify some elements and then let the tail free:

[1, 2|A]

BTW, you can see that for yourself by using write_canonical/1:

?- write_canonical([1, 2, 3, 4]).
'.'(1,'.'(2,'.'(3,'.'(4,[]))))
true.

Hope it helped

m09
  • 7,490
  • 3
  • 31
  • 58