Advertisement
Guest User

tail recursive list concat

a guest
Apr 9th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.49 KB | None | 0 0
  1. (* revappend takes two 'a lists as arguments and concatenates them
  2.  * with the side effect of reversing the order of the first list. *)
  3. let rec revappend l1 l2 =
  4.    match l1 with
  5.    | [] -> l2
  6.    | h :: t -> revappend t (h::l2)
  7.  
  8. (* reverse a list *)
  9. let reverse l = revappend l []
  10.  
  11. (* Concatonate 'a list list -> 'a list should be tail recursive*)
  12. let concat ll =
  13.    let rec aux ll l =
  14.       match ll with
  15.       | [] -> l
  16.       | h :: t -> aux t (revappend h l)
  17.    in reverse (aux ll [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement