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 GAA14373; Sat, 21 Apr 2001 06:30:54 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id GAA14527 for ; Sat, 21 Apr 2001 06:30:53 +0200 (MET DST) Received: from fledge.watson.org (fledge.watson.org [204.156.12.50]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f3L4Upv10764 for ; Sat, 21 Apr 2001 06:30:51 +0200 (MET DST) Received: from localhost (patrick@localhost) by fledge.watson.org (8.11.3/8.11.3) with SMTP id f3L4VMm52107; Sat, 21 Apr 2001 00:31:22 -0400 (EDT) (envelope-from patrick@watson.org) Date: Sat, 21 Apr 2001 00:31:22 -0400 (EDT) From: Patrick M Doane To: "markus.kliegl" cc: Caml List Subject: Re: [Caml-list] Random results In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi Markus, You have an interesting problem in the code that is hard to spot at first. I'll interleave some comments below: On Sat, 21 Apr 2001, markus.kliegl wrote: > let rot13_char c = > let x = int_of_char c in > if (x >= 65 && x < 78) || (x >= 97 && x < 110) then > char_of_int (x + 13) > else if (x >= 78 && x < 91) || (x >= 110 && x < 123) then > char_of_int (x - 13) > else c > ;; Note that rot13_char (rot13_char c) == c > let rot13_str str = > for i = 0 to String.length str - 1 do > str.[i] <- (rot13_char str.[i]) > done; > str > ;; String updates in Caml are destructive, and strings are passed "by reference" to use C++ terminology. So this means that: (rot13_str s) == s which may not be expected. > let rec beer n = > if n = 1 then begin > rot_print "1 bottle of beer on the wall"; > rot_print "1 bottle of beer"; > rot_print "Take one down and pass it around"; > rot_print "No bottles of beer on the wall" > end else begin > print_int n; rot_print " bottles of beer on the wall"; > print_int n; rot_print " bottles of beer"; > rot_print "Take one down and pass it around"; > print_int (n - 1); rot_print " bottles of beer on the wall"; > print_newline (); > beer (n - 1) > end > ;; Since rot13_str destructively modifies its argument, the string literals are getting modified. Each invokation of beer for n > 2 will alternate between rot13 and plaintext representation. Patrick Doane ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr