Re: Need hints for little exercise : write the List.concat
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
On 20/04/2016 16:15, Gabriel Scherer [email protected] [ocaml_beginners] wrote: > > You might find it easier to start by writing > > val sum : int list -> int > > (returning 0 on the empty list) and then "generalize" this to > > val concat : 'a list list -> 'a list > Thanks to your advice, I tried to generalize the sum function so I did this let rec myappend first second= match first with |[] -> second |hd :: tl -> hd :: (myappend tl second);; val myappend : 'a list -> 'a list -> 'a list = <fun> then let rec myconcat alist = match alist with |[] -> [] |hd ::tl -> myappend hd (myconcat tl);; val myconcat : 'a list list -> 'a list = <fun> and I tried to do like I wanted to in my first try: let rec myconcat2 alist = match alist with |[] -> [] |hd :: tl -> let rec aux first second= match first with |[] -> second |tete ::queue -> tete :: (aux queue second) in aux hd (myconcat2 tl);; val myconcat2 : 'a list list -> 'a list = <fun> > There are interesting, slightly more advanced discussions to be had > over the specific way you implemented the previous functions (some > correct definitions are more correct than the other) > What do you mean?