How does recursion in Prolog works from inside. One example

后端 未结 4 1163
悲哀的现实
悲哀的现实 2021-01-19 10:36

I\'ve got here a small script, that converts list of elements into a set. For example list [1,1,2,3] -> set [1,2,3]. Can somebody explain to me, step by step, whats happenin

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-19 11:30

    In certain implementations of Prolog, e.g. GNU Prolog, you can trace the execution of your code. Using this feature you can step through the evaluation process, as demonstrated below.

    $ gprolog
    GNU Prolog 1.4.2
    By Daniel Diaz
    Copyright (C) 1999-2012 Daniel Diaz
    | ?- consult('list_to_set.pro').
    yes
    | ?- trace.
    yes
    {trace}
    | ?- list_to_set([1,1,2,3], X).
          1    1  Call: list_to_set([1,1,2,3],_25) ?
          2    2  Call: list_to_set([1,2,3],_57) ?
          3    3  Call: list_to_set([2,3],_83) ?
          4    4  Call: list_to_set([3],_109) ?
          5    5  Call: list_to_set([],_135) ?
          5    5  Exit: list_to_set([],[]) ?
          6    5  Call: \+member(3,[]) ?
          7    6  Call: member(3,[]) ?
          7    6  Fail: member(3,[]) ?
          6    5  Exit: \+member(3,[]) ?
          4    4  Exit: list_to_set([3],[3]) ?
          7    4  Call: \+member(2,[3]) ?
          8    5  Call: member(2,[3]) ?
          ...
    

    An explanation of how to interpret Prolog's traces can be found at http://remus.rutgers.edu/cs314/f2007/ryder/projects/prolog/prologTrace.html, section READING A TRACE.

提交回复
热议问题