Programmation Fonctionnelle, TP3

Version & licenses
Creative Commons License

Programmation Fonctionnelle, TP3 : stream_lzw.ml

Guyslain Naves
  1. type 'a t = Cons of 'a option * ('a t Lazy.t)


  2. let rec uniform elt = Cons (elt, lazy (uniform elt))

  3. let is_empty = function
  4. | Cons (None,_) -> true
  5. | _ -> false

  6. let read = function
  7. | Cons (Some thing,next) -> (thing, Lazy.force next)
  8. | _ -> failwith "End of stream"

  9. let rec map f = function
  10. | Cons (None,next) -> Cons (None, lazy (map f (Lazy.force next)))
  11. | Cons (Some thing, next) -> Cons (Some (f thing), lazy (map f (Lazy.force next)))


  12. let rec flatten = function
  13. | Cons (Some (head::tail),stream) -> Cons(Some head, lazy (flatten (Cons (Some tail,stream))))
  14. | Cons (Some ([]),stream) -> flatten (Lazy.force stream)
  15. | Cons (None,stream) -> uniform None

  16. let rec of_in_channel chan =
  17. try
  18. Cons (Some (input_char chan), lazy (of_in_channel chan))
  19. with
  20. | _ -> uniform None