Need hints for little exercise : write the List.concat
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I am doing the Ocaml exercises from http://exercism.io/, I am at the
part list-ops where you must rewrite some basics
List functions.
I have already done those ones (that was not easy I fell like I am retarded)
val length : 'a list -> int val reverse : 'a list -> 'a list
val map : f:('a -> 'b) -> 'a list -> 'b list
val filter : f:('a -> bool) -> 'a list -> 'a list
val fold : init:'acc -> f:('acc -> 'a -> 'acc) -> 'a list -> 'acc
val append : 'a list -> 'a list -> 'a list
But now I am stuck with this one:
val concat : 'a list list -> 'a list
I don't want the answer (I want to find it by myself) but just a hint
that can help me.
For example I was thinking of taking recursively the sub-lists two by
two and applying an "append"
function on them. Something like this :
let rec concat alist =
let rec aux f s =
match f with
| [] -> []
| hd :: tl -> hd :: (aux tl s) in
match alist with
| [] -> []
| hd :: mid :: tl -> (aux hd mid) :: concat tl
This code doesn't work and I have the feeling that it is simpler than that.
Thanks