I was creating a simple concat, and thought that this might work.
(** [foldl fun init lst] the [fun] gets applied, left to right:
{[foldl fun init [e1;e2;e3;e4] -> fun e4 (fun e3 (fun e2 (fun e1 init)))]} *)
let foldl func = let rec foldl accum = function
|[] -> accum
|(head :: tail) -> foldl (func head accum) tail
in foldl
(** [reverse lst] reverses [lst]. Useful for tail recursive list building functions *)
let reverse : 'a list -> 'a list = foldl (fun a b -> a :: b) []
let concat lst = lst |> reverse |> foldl (foldl (fun a b -> a :: b)) [] |> reverse
This only works when I try this
(** [foldl fun init lst] the [fun] gets applied, left to right:
{[foldl fun init [e1;e2;e3;e4] -> fun e4 (fun e3 (fun e2 (fun e1 init)))]} *)
let foldl func = let rec foldl accum = function
|[] -> accum
|(head :: tail) -> foldl (func head accum) tail
in foldl
(** [reverse lst] reverses [lst]. Useful for tail recursive list building functions *)
let reverse : 'a list -> 'a list = foldl (fun a b -> a :: b) []
let reverse' : 'a list -> 'a list = foldl (fun a b -> a :: b) []
let concat lst = lst |> reverse' |> foldl (foldl (fun a b -> a :: b)) [] |> reverse
I heard this was related to the value restriction, but I don't know how to make it work. What should I do?