caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Diego Olivier Fernandez Pons" <FernandezPons@iFrance.com>
To: "Caml" <caml-list@inria.fr>
Subject: [Caml-list] Pattern matching
Date: Thu, 8 Nov 2001 03:33:29 +0100	[thread overview]
Message-ID: <009f01c167fe$ebf5e780$2770f2c3@Utilisateur> (raw)

An english translation follows

Pourrait quelqu'un expliquer les raisons pour lesquelles dans un
pattern matching toutes les variables introduites sont considérées
comme de nouvelles variables ? Est-ce pour des questions de choix
entre égalité physique et égalité structurelle ?

En effet, le code suivant :

(* couples 4 = (1,2) (1,3) (1,4) (2,3) (2,4) (3,4) *)
let couples = function n ->
  let rec couplesCPS = function
    | (n,_) -> [ ]
    | (i,n) -> (i,n) :: couplesCPS (i+1, i+2)
    | (i,j) -> (i,j) :: couplesCPS (i, j+1)
  in    couplesCPS (1,2)
;;

me semble nettement plus lisible que le code qu'il faut écrire en Caml
actuellement :

(* couples 4 = (1,2) (1,3) (1,4) (2,3) (2,4) (3,4) *)
let couples = function n ->
  let rec couplesCPS = function
    | (i,_) when (i = n) -> [ ]
    | (i,j) when (j = n) -> (i,j) :: couplesCPS (i+1, i+2)
    | (i,j) -> (i,j) :: couplesCPS (i, j+1)
  in    couplesCPS (1,2)
;;

Bien sûr, si l'on passait des listes on aurait le choix entre les deux
types d'égalité, mais on pourrait toujours définir une syntaxe
alternative pour le cas de l'égalité physique par exemple (laisser le
when pour ce cas là et autres types de comparaisons...)

Pourquoi demande-t-on à un motif d'être « linéaire » ? (c'est à dire
si je comprends bien la documentation qu'une variable ne peut
apparaître qu'une fois dans un motif). Il est bien évident que
certains motifs ne doivent pas être permis

let egalite = function
   | (x,x) -> true
   | (x,y) -> false
;;
# This variable is bound several times in this matching

mais x est ici une nouvelle variable ce qui n'était pas le cas de n
dans la fonction couple.

    Diego Olivier

< Your function will always return 1, because variables on the
< left-hand side of a pattern-match are bound like in a let
< expression.

Could someone explain why are variables on the left-side of a
pattern-match bound like in a let expression ? Is it a problem of
structural versus physical equality ? In which case, why not choosing
structural by default and leaving physical for the « when » syntax ?

« Patterns are linear: a variable cannot appear several times in a
given pattern. In particular, there is no way to test for equality
between two parts of a data structure using only a pattern (but when
guards can be used for this purpose). » (manual 6.6)
Could someone explain why must patterns be linear ?


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


             reply	other threads:[~2001-11-09  0:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-11-08  2:33 Diego Olivier Fernandez Pons [this message]
2001-11-09  0:26 ` Pixel
2001-11-09 10:59   ` Luc Maranget
     [not found] ` <15339.34220.198731.791811@lachesis.inria.fr>
2001-11-10  2:16   ` Diego Olivier Fernandez Pons
2001-11-12 10:29     ` Luc Maranget
2001-11-12  9:00       ` Diego Olivier Fernandez Pons
2001-11-13  8:00         ` Fabrice Le Fessant
2001-11-13 23:57           ` [Caml-list] If ou Pattern-matching ? Diego Olivier Fernandez Pons
2001-11-14 10:02             ` Fabrice Le Fessant
2001-11-14 10:47             ` Nicolas Barnier
2001-11-14  1:26               ` [Caml-list] Warnings possibles Diego Olivier Fernandez Pons
2001-11-14 18:24                 ` Luc Maranget
2001-11-14 11:35             ` [Caml-list] If ou Pattern-matching ? Luc Maranget
2001-11-13 16:09         ` [Caml-list] Pattern matching Luc Maranget
2001-11-13 23:56           ` [Caml-list] Deux types de pattern-matching ? Diego Olivier Fernandez Pons
2001-11-14 10:19             ` [Caml-list] " Luc Maranget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='009f01c167fe$ebf5e780$2770f2c3@Utilisateur' \
    --to=fernandezpons@ifrance.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).