Re: pattern matching with keyword function

"'Mr. Herr' [email protected] [ocaml_beginners]" <[email protected]> Fri, 8 Apr 2016 11:08:42 +0200
Newsgroups gmane.comp.lang.ocaml.beginners
Message-ID <[email protected]>

On 08.04.2016 10:50, cedlemo [email protected] [ocaml_beginners] wrote:
>  
>
> Hi,
>
> I have "stumbled" on this simple function
>
> let rec flatten = function
> [] -> []
> | l::r -> l @ flatten r
>
> At first glance I asked myself why they don't use the last remaining
> element of the list
> then I see that the line
> [] -> []
>
> doesn't have any "|" at the beginning.
>
> Could you explain we how does that works?
> And maybe could you give me one or more links for informations/examples
> on the patterns matching
> with the function keywords.
>
In pattern matching, the first vertical bar is optional.
Look at this, it is "normal" not to have a bar after "function":

let rec flatten = function [] -> [] | h :: t -> h @ flatten t

It works like this:

take the first sublist from the list, and list-append it to the next sublist,
until the list is done. A sequence of List.cons - operator :: - always terminates in [].

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

/Str.