How to generate recursive list in OCaml

前端 未结 5 889
抹茶落季
抹茶落季 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:51

    In an eager language like ML, you need to use streams. For example

    # let cycle = Stream.from (fun n -> Some (List.nth [1;2;3] (n mod 3)));;
    val cycle : int Stream.t = 
    # Stream.npeek 10 cycle;;
    - : int list = [1; 2; 3; 1; 2; 3; 1; 2; 3; 1]
    

提交回复
热议问题