Version française
Home     About     Download     Resources     Contact us    
Browse thread
match values
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Quercia <querciam@l...>
Subject: match values
If you can't read french, please do skip the 51 following lines.

En francais :

Le code Caml suivant ne fonctionne pas comme on pourrait le vouloir :

     let f(x,y,z) = match x with
     | y -> ...
     | z -> ...
     | _ -> ...
     ;;

le compilateur signale que les cas 2 et 3 sont inutiles car le "y"
du match est pris comme un identificateur nouveau et non comme le
deuxieme parametre de "f".

La solution est d'ecrire :

     let f(x,y,z) =
          if x = y then ...
     else if x = z then ...
     else ...
     ;;

ou aussi :

     let f(x,y,z) = match 0 with
     | _ when x=y -> ...
     | _ when x=z -> ...
     | _ -> ...
     ;;

mais ces deux formes ne me plaisent pas !
Peut-on envisager ?

1. de modifier la syntaxe de "match" de sorte que les identificateurs
soient evalues s'ils ne sont pas precedes de "as" comme dans :

     match x with
     | y      -> ... (* comparaison des valeurs x et y   *)
     | _ as t -> ... (* definition de l'identificateur t *)

1'. sans pour autant modifier les autres formes de filtrage comme :

     let (u,v) = ... in ... (* definition des identificateurs u et v *)
     let f(u,v) = ... (* idem *)

Il est probable que 1 et 1' sont inconciliables ?

2. d'introduire une nouvelle forme de filtrage "matchval" qui evaluerait
les motifs avant de proceder a la comparaison, en conservant la
possibilite
de definir un nouvel identificateur par "_ as <ident>" ?

==============================================================================

In english :

The following Caml code does not work as one would expect :

     let f(x,y,z) = match x with
     | y -> ...
     | z -> ...
     | _ -> ...
     ;;

the compiler warns that cases #2 & #3 are useless because "y" in the
match
construct is interpreted as a new identifier and not as the second
parameter
of "f".

The solution is to write :

     let f(x,y,z) =
          if x = y then ...
     else if x = z then ...
     else ...
     ;;

or :

     let f(x,y,z) = match 0 with
     | _ when x=y -> ...
     | _ when x=z -> ...
     | _ -> ...
     ;;

but I don't like these constructs !
May I dream about ?

1. modify "match" syntax so that identifiers are evaluated unless they
are
precededed by "as" as in :

     match x with
     | y      -> ... (* comparison of the values of x and y *)
     | _ as t -> ... (* definition of identifier t          *)

1'. without modifying other patern-matching forms as :

     let (u,v) = ... in ... (* definition of identifiers u and v *)
     let f(u,v) = ... (* idem *)

I think that 1 and 1' are incompatible ?

2. introduce a new form of pattern-matching "matchval" which would
evaluate
the patterns before comparison, preserving the possibility to define a
new
identifier with a "_ as <ident>" construct ?

-- 
Michel Quercia
Lycee Carnot  16 bd Thiers  21000 Dijon
e-mail = querciam@l-carnot1.ac-dijon.fr