7

I have been trying to learn Prolog, and am totally stumped on what the predicate s() does. I see it used often and there is so little resources on the internet about Prolog that I cannot find an answer.

Ex.

    /* sum(Is,S) is true if S is the sum of the list of integers Is.           */
    sum([],0).
    sum([0|Is],S):-sum(Is,S).
    sum([s(I)|Is], s(Z) ):-sum([I|Is],Z).
false
  • 10,264
  • 13
  • 101
  • 209
okin33
  • 144
  • 1
  • 9

3 Answers3

16

s/1 does not do anything in itself, and it's not really a predicate. They are just terms, a representation of the successor of their argument. So, s(0) is used to represent the successor of 0 (i.e. 1), s(s(0)) is used to represent the successor of s(0) (i.e. 2), and so on and so forth. They are so widespread in Prolog because Prolog is quite fine a language to perform symbolic computation, whereas even simple arithmetic operations feel clunky, meaning that they are not seamlessly integrated with the programming paradigm.

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
  • 1
    This explains, very well, why the output of my programs using s/1 were so strange. Thank you very much, great explanation! – okin33 Nov 19 '11 at 21:59
1

s/1 stands for successor. It's used to represent numbers in a logically accessible ways.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • 1
    That makes sense thank you very much! However when I try run a program count that uses s/1 I get strange output in the form of: ?- mycount(a,[a,a],N). N = s(s(0)) ; N = s(0) ; N = s(0) ; N = 0 ; false. – okin33 Nov 19 '11 at 21:02
0

It is Prolog-implementation specific. It refers to a successor-predicate, see this for some more info

slashmais
  • 7,069
  • 9
  • 54
  • 80
  • 1
    `s(X)` is not implementation specific. It's an arbitrary functor. Try `t(X)` or `succ(X)` in its place. – false Nov 25 '11 at 13:57