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 WAA07517; Sun, 23 Feb 2003 22:17:24 +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 WAA07407 for ; Sun, 23 Feb 2003 22:17:23 +0100 (MET) Received: from mel-rto3.wanadoo.fr (smtp-out-3.wanadoo.fr [193.252.19.233]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h1NLHMH12155 for ; Sun, 23 Feb 2003 22:17:22 +0100 (MET) Received: from mel-rta10.wanadoo.fr (193.252.19.193) by mel-rto3.wanadoo.fr (6.7.015) id 3E0C33B5024DCD6D for caml-list@inria.fr; Sun, 23 Feb 2003 22:17:22 +0100 Received: from debian (80.8.74.208) by mel-rta10.wanadoo.fr (6.7.015) id 3E26DAA601588B5C for caml-list@inria.fr; Sun, 23 Feb 2003 22:17:22 +0100 Received: from moi by debian with local (Exim 3.36 #1 (Debian)) id 18n3Db-0000nI-00 for ; Sun, 23 Feb 2003 21:59:27 +0100 To: caml-list@inria.fr Subject: Re: [Caml-list] One question Mail-Copy-To: never From: Remi Vanicat Date: Sun, 23 Feb 2003 21:59:27 +0100 In-Reply-To: <1046017748.12274.14.camel@kvip88.KVI.nl> ("Alexander S. Usov"'s message of "Sun, 23 Feb 2003 17:29:14 +0100") Message-ID: <87wujqvhlc.dlv@wanadoo.fr> User-Agent: Gnus/5.090014 (Oort Gnus v0.14) Emacs/21.2 (i386-pc-linux-gnu) References: <1046017748.12274.14.camel@kvip88.KVI.nl> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-15 Content-Transfer-Encoding: 8bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk "Alexander S. Usov" writes: > Hi! > > Can anybody expalin me why in such code > > ------ > type lex_node = Letter of char * bool * lex_tree > and lex_tree = lex_node list ;; > > exceprion Already ;; > > let rec insert lex word = > let whd = word.[0] > and wtl = String.sub word 1 (String.length word - 1) > in > try > match lex with > | (Letter(c,b,t) as letter)::tail when (c <> whd) -> > letter :: (insert tail word) > | Letter(c,b,t) :: tail when (wtl = "") -> > if b = true then > raise Already > else > Letter(c,true,t) :: tail > | Letter(c,b,t) :: tail -> > Letter(c,b,(insert t wtl)) :: tail > | [] -> > if wtl = "" then > [ Letter (whd, true, []) ] > else > [ Letter (whd, false, insert [] wtl) ] > with > | Already -> lex ;; > > let a = insert [] "word" > in > a == insert a "word" ;; > ----- > > returns false? a #trace insert make it very clear to me : it's because the Already exception is immediately caught. your code do exactly the same than : let rec insert lex word = let whd = word.[0] and wtl = String.sub word 1 (String.length word - 1) in match lex with | (Letter(c,b,t) as letter)::tail when (c <> whd) -> letter :: (insert tail word) | Letter(c,b,t) :: tail when (wtl = "") -> if b = true then lex else Letter(c,true,t) :: tail | Letter(c,b,t) :: tail -> Letter(c,b,(insert t wtl)) :: tail | [] -> if wtl = "" then [ Letter (whd, true, []) ] else [ Letter (whd, false, insert [] wtl) ] you should catch the exception at the top most part of the insertion. -- Rémi Vanicat vanicat@labri.u-bordeaux.fr http://dept-info.labri.u-bordeaux.fr/~vanicat ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners