Prolog - generating numbers fitting given range

前端 未结 3 1338
情歌与酒
情歌与酒 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:18

    SWI-Prolog has the predicate between/3. so you would call it like between(0,5,X) to get the results you showed above. This predicate looks like this is implemented in C though.

    If we have to write it in pure prolog (and speed&space is not a factor), you could try this following.

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

提交回复
热议问题