函数seq应该返回从init开始的序列,并且在每次按步骤扩展,直到条件未被验证为止:
let rec seq init step cond () =
let r = ref init in
if cond !r
then Cons(step !r, seq !r step cond)
else Nil 我尝试了这些(to_list (seq1seq1(趣味x -> x+1) )(趣味x -> x<10),并且在计算期间有堆栈溢出(循环递归?)。
发布于 2018-04-24 11:30:50
首先,你应该避免在没有充分理由的情况下使用引用。那么,这只是存储当前值的问题,而不是下一个值的问题:
let rec seq current step cond () =
if cond current then
Cons(current, seq (step current) step cond)
else
Nilhttps://stackoverflow.com/questions/49997241
复制相似问题