Re: pattern matching with keyword function
| Newsgroups | gmane.comp.lang.ocaml.beginners |
|---|---|
| Message-ID | <[email protected]> |
On 08.04.2016 12:36, cedlemo [email protected] [ocaml_beginners] wrote: > > > > In pattern matching, the first vertical bar is optional. > Look at this, it is "normal" not to have a bar after "function": > > Thanks that is the principal information that I needed. > > > More info: try to start reading the fine manual, because once you mastered reading > type signatures you get authoritative information. First stop for more OCaml info > is ocaml.org > > I have the "real worl ocaml" book and I know the main site too, the code I shown > you came > from : https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.ml > > In the same file I have this function too: > > let rec map f = function > [] -> [] > | a::l -> let r = f a in r :: map f l > > And I don't get something : > > assuming we do something like that: > a_fn is a function > a_list is a two element list > > map a_fn a_list > > in the first call of the map function, we go through the line: > a::l -> let r = f a in r :: map f l > > the first element is taken and we use it as an argument to the a_fn function. The > result is then added to the result of the instruction > > map f l which trigger the line > [] -> [] > > but it look like the a_fn is not applied to the last element. > > What am I missing ? > Maybe we just rewrite map (short names are idiomatic, but l as variable name is a bad choice IMO, prints like capital i): let rec map f ls = match ls with [] -> [] | elem :: rest -> let temp = f elem in temp :: map f rest Do you still think f is not applied to the last element? The empty list is not the last element, but the end indicator. /Str.