1

function seq should return a sequence starting from init and at each time extends by step untill the condition is not verified:

let  rec seq init step cond () = 
let r = ref init in
if cond !r 
then  Cons(step !r, seq !r step cond)
else Nil 

i tried these (to_list (seq 1 seq 1 (fun x -> x+1) (fun x -> x <10)) and i have Stack overflow during evaluation (looping recursion?).

4
  • 2
    r is never updated Commented Apr 24, 2018 at 8:53
  • comment not related to your SO : why do you use () in let rec seq init step cond () ? why not simply let rec seq init step cond ? Commented Apr 24, 2018 at 9:58
  • if i use only like seq init step cond like you said it will return a 'a node not an 'a t Commented Apr 24, 2018 at 10:33
  • OK - I see, it is related to the other ticket you have posted (stackoverflow.com/questions/49636168/…) in which you define those types. Commented Apr 24, 2018 at 11:25

1 Answer 1

4

First, you should avoid using reference without good reason. Then it is just a matter of storing the current value and not the next one:

let rec seq current step cond () =
if cond current then 
  Cons(current, seq (step current) step cond)
else
  Nil
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.