From: Patrick M Doane <patrick@watson.org>
To: "markus.kliegl" <markus.kliegl@t-online.de>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Random results
Date: Sat, 21 Apr 2001 00:31:22 -0400 (EDT) [thread overview]
Message-ID: <Pine.BSF.3.96.1010421002527.51839A-100000@fledge.watson.org> (raw)
In-Reply-To: <Pine.LNX.4.10.10104210456190.26953-100000@linux.magictux.net>
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
next prev parent reply other threads:[~2001-04-21 4:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-04-21 3:25 markus.kliegl
2001-04-21 4:31 ` Patrick M Doane [this message]
[not found] <20010420211305.A7171@opus.davidb.org>
2001-04-21 4:33 ` markus.kliegl
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=Pine.BSF.3.96.1010421002527.51839A-100000@fledge.watson.org \
--to=patrick@watson.org \
--cc=caml-list@inria.fr \
--cc=markus.kliegl@t-online.de \
/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