From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id BAA31081; Fri, 9 Nov 2001 01:07:26 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id BAA31586 for ; Fri, 9 Nov 2001 01:07:25 +0100 (MET) Received: from transmail12.infosources.fr (transmail12.infosources.fr [212.232.33.78]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id fA907Ov23808 for ; Fri, 9 Nov 2001 01:07:24 +0100 (MET) Received: from infonie.fr (mailbox.infonie.fr [195.242.64.77]) by transmail12.infosources.fr (8.11.6/8.11.6) with SMTP id fA907O125414 for ; Fri, 9 Nov 2001 01:07:24 +0100 (MET) Received: from Utilisateur ( Unverified [195.242.112.39] ) by infonie.fr with SMTP id 27291.1012160.455448; Fri, 9 Nov 2001 01:07:23 +0100 Message-ID: <009f01c167fe$ebf5e780$2770f2c3@Utilisateur> From: "Diego Olivier Fernandez Pons" To: "Caml" Subject: [Caml-list] Pattern matching Date: Thu, 8 Nov 2001 03:33:29 +0100 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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