Re: last item. no output
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
Matthieu Dubuget [email protected] [ocaml_beginners] schreef op 18-10-2014 14:14: > OK. Now you're problem is much clear. > > Have a close look at the type inferred for last. > > val last : 'a list list -> 'a list = <fun> > > This is due to your first match case: if your incoming list is void, you are returning a void list as last element. > > It has to return lists in the other cases. So does it. > > Which means that last is waiting for a list of list of something: 'a list list > > > The problem is asking you to take a list of something ('a list), and return either that there is no last > element (None) or that there is one element (Some something). > > val last: 'a list -> 'a option > > Have a look at what option is. > > Salutations > Thanks, This one is working well : let rec last list = match list with | [] -> None | [x] -> Some x | hd :: tl -> last tl ;; Roelof ------------------------------------ ------------------------------------ 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 <*> To visit your group on the web, go to: http://groups.yahoo.com/group/ocaml_beginners/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/ocaml_beginners/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/