What does the s() predicate do in Prolog?

匆匆过客 提交于 2019-12-21 11:55:07

问题


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).

回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/8197088/what-does-the-s-predicate-do-in-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!