How to generate recursive list in OCaml

前端 未结 5 886
抹茶落季
抹茶落季 2021-01-21 06:05

I would like to implement analog of Haskell cycle function.

If I pass list elements explicitly it seems trivial:

let cycle a b c =
  let rec         


        
5条回答
  •  被撕碎了的回忆
    2021-01-21 06:41

    You need streams as in another answer, or lazy lists:

    type 'a llist = LNil | LCons of 'a * 'a llist Lazy.t
    let cycle = function
    | [] -> invalid_arg "cycle: empty list"
    | hd::tl ->
      let rec result =
        LCons (hd, lazy (aux tl))
      and aux = function
        | [] -> result
        | x::xs -> LCons (x, lazy (aux xs)) in
      result
    

提交回复
热议问题