Re: Need hints for little exercise : write the List.concat
"Gabriel Scherer [email protected] [ocaml_beginners]" <[email protected]> Wed, 20 Apr 2016 10:15:27 -0400
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <CAPFanBF=+FT-1fSTKboU36N1mzSZ47UhTxhVSk8FSCZTR=h54Q@mail.gmail.com> |
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 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), don't hesitate to come back to this list after you feel a bit more confident on those exercises. On Wed, Apr 20, 2016 at 9:57 AM, cedlemo [email protected] [ocaml_beginners] <[email protected]> wrote: > 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 > > > > ------------------------------------ > Posted by: cedlemo <[email protected]> > ------------------------------------ > > Archives up to December 31, 2011 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners > The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr > Attachments are banned and you're asked to be polite, avoid flames etc. > ------------------------------------ > > Yahoo Groups Links > > >