| Newsgroups |
gmane.comp.lang.ocaml.beginners |
| Message-ID |
<CAPFanBE1aH6n7Huet4uVAyZBje7Ym3A56MPdqNz8s4QC0JbVMQ@mail.gmail.com> |
I second the use of "begin..end" in nested matches. I like this form
because it is closer to Coq's pattern-matching syntax, which is simply
match t with
...
end
(so "match" has an explicit "end" delimiter).
This is the right syntax, and if we had known when OCaml was designed
we should have used it. (... or using layout-dependent rules that also
have this explicit delimiter, but in a way that is less heavy to the
eye.)
To emulate it, each time I'm using "match" inside a pattern-matching
clause, I write
| p ->
...
begin match t with
...
end
...
Note that the "begin" is one the same line as the "match", together
they almost form a single keyword, so this is very close to the Coq
syntax.
This is, however, unnecessary if the inner-match is inside a
construction that is already has both opening and closing keywords
(typically in the dots of "then begin ... end" or "else begin ...
end"), as conditionals mandate their own parentheses for similar
reasons, or "let x = ... in").
On Fri, Oct 9, 2015 at 4:24 PM, Oliver Bandel
[email protected] [ocaml_beginners]
<[email protected]> wrote:
>
> Zitat von "Seung-jin Kim [email protected] [ocaml_beginners]"
> <[email protected]> (Fri, 9 Oct 2015 12:55:53 +0900)
>
>> First of all,
>> There was a typo in my very initial question.
>> my has_element1 should be
>>
>> let rec has_element1 l e =
>> match l with
>> | [] -> false
>> | h::t -> if e = h then true else has_element1 t e;;
>>
>> Anyway,, Seems everyone got my point. :-) First time to post this
>> group and very new to ocaml.
>>
>> I did with ( ) for my second match.
>>
>> utop[91]> let rec has_element2 l e =
>> match l with
>> | [] -> false
>> | h::t -> ( match h with
>> | e -> true
>> | _ -> has_element2 t e
>> )
>> ;;
>
> I would prefer using indentation and to align hte '(' and the ')'
> vertically.
>
> Also I would even more prefer "begin" and "end" over '(' and ')'.
> Thats a readability-issue.
>
> But it seems, your problem could be solved without a second
> match-statement.
> But, just in case, one day you will need a nested one,
> you may remember my comment here.
>
> Of course it's a matter of taste, what I mendtioned here.
>
> Ciao,
> Oliver
>
>
>
> ------------------------------------
> Posted by: Oliver Bandel <[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
>
>
>