Implementing “last” in Prolog

后端 未结 3 1879
暖寄归人
暖寄归人 2020-11-30 15:05

I am trying to get a feel for Prolog programming by going through Ulle Endriss\' lecture notes. When my solution to an exercise does not behave as expected, I find it diffi

3条回答
  •  余生分开走
    2020-11-30 15:49

    this code would work:

    last1([Last], Last).
    last1([_ | Rest], Last) :- last1(Rest, Last), !.
    

    it is because prolog things there might be more combinations but, with this symbol: !, prolog won't go back after reaching this point

提交回复
热议问题