Reputation: 11331
I am implementing this hoemwork functionality using Ocaml:
List
module'a list list -> 'a list
[[1,2,3],[45]] => [1,2,3,4,5]
and [[[1,2,3],[4,5]],[[6,7]]] => [[1,2,3],[4,5],[6,7]]
I am not sure where to start, can anyone give me some suggestion? Thank you
Upvotes: 0
Views: 8316
Reputation: 66808
Thomas has given excellent advice. Your basic operation is going to be to append one list to another. It might help to write this function as a separate function first. It will look something like this:
let rec myappend a b =
match a with
| [] -> (* Empty list prefixed to b (easy case) *)
| ahead :: atail -> (* Recursive case *)
Armed with your own append function, you can carry out another level of recursion to append all the top-level lists as Thomas suggests.
Upvotes: 2
Reputation: 5048
I don't see the difference between List.flatten
and your function.
To answer to your question: as usual with lists, try to think about the base cases:
Wrap everything into a pattern match, cook it for few hours, and that's done :-)
Upvotes: 6