Prolog - generating numbers fitting given range

前端 未结 3 1329
情歌与酒
情歌与酒 2021-01-02 05:31

I\'d like to use predicates like:

range(X,0,5)
range(X,4,200)
range(X,-1000000,1000000)
dom_range(X,-1000000,1000000)

with meaning :

<
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 06:06

    Dave's answer is almost perfect: there is no check to see if low < high. I added a condition and now it works fine (otherwise it generates numbers from low to infinity):

    range(Low, Low, High).
    range(Out,Low,High) :- NewLow is Low+1, NewLow =< High, range(Out, NewLow, High).
    

    Hope that helps!

提交回复
热议问题