Convert peano number s(N) to integer in Prolog

前端 未结 3 1503
Happy的楠姐
Happy的楠姐 2020-12-07 01:55

I came across this natural number evaluation of logical numbers in a tutorial and it\'s been giving me some headache:

natural_number(0).
natural_number(s(N))         


        
3条回答
  •  臣服心动
    2020-12-07 02:34

    Here is another solution that works "both ways" using library(clpfd) of SWI, YAP, or SICStus

    :- use_module(library(clpfd)).
    
    natsx_int(0, 0).
    natsx_int(s(N), I1) :-
       I1 #> 0,
       I2 #= I1 - 1,
       natsx_int(N, I2).
    

提交回复
热议问题