caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Random results
@ 2001-04-21  3:25 markus.kliegl
  2001-04-21  4:31 ` Patrick M Doane
  0 siblings, 1 reply; 3+ messages in thread
From: markus.kliegl @ 2001-04-21  3:25 UTC (permalink / raw)
  To: Caml List

Hi,

Sorry for the length of the message.

I have the following program:
let rot = ref false;;

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
;;

let rot13_str str =
  for i = 0 to String.length str - 1 do
    str.[i] <- (rot13_char str.[i])
  done;
  str
;;

let rot_print str =
  if !rot = false then
    print_endline str
  else
    print_endline (rot13_str str)
;;

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
;;

let main argc argv =
  if (argc > 1) && (argv.(1) = "rot13") then
    rot := true;
  beer 99
;;

main (Array.length Sys.argv) Sys.argv

When running it without the optional argument 'rot13', the
output is correct. However, when I do run it with that argument,
I get results in which some lines are rot13-ed and others not,
e.g.:
3 obggyrf bs orre ba gur jnyy
3 obggyrf bs orre
Gnxr bar qbja naq cnff vg nebhaq
2 obggyrf bs orre ba gur jnyy

2 bottles of beer on the wall
2 bottles of beer
Take one down and pass it around
1 bottles of beer on the wall

1 obggyr bs orre ba gur jnyy
1 obggyr bs orre
Gnxr bar qbja naq cnff vg nebhaq
Ab obggyrf bs orre ba gur jnyy

I'm using Ocaml 3.00, by the way. Is this a bug in my program
(I can't find it) or in Ocaml?

Thanks,
Markus Kliegl

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Random results
  2001-04-21  3:25 [Caml-list] Random results markus.kliegl
@ 2001-04-21  4:31 ` Patrick M Doane
  0 siblings, 0 replies; 3+ messages in thread
From: Patrick M Doane @ 2001-04-21  4:31 UTC (permalink / raw)
  To: markus.kliegl; +Cc: Caml List

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Random results
       [not found] <20010420211305.A7171@opus.davidb.org>
@ 2001-04-21  4:33 ` markus.kliegl
  0 siblings, 0 replies; 3+ messages in thread
From: markus.kliegl @ 2001-04-21  4:33 UTC (permalink / raw)
  To: David Brown; +Cc: Caml List


On Fri, 20 Apr 2001, David Brown wrote:

> 
> Ocaml appears to only have one copy of each string.  When you rot13 the
> string, it overwrite the constant.  This toggles.
> 
> An easy fix is to copy the string before changing the characters:
> 
> let rot13_str str =
>   let str = String.copy str in
>   for i ...

Works perfectly, thanks!

Markus Kliegl

-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2001-04-21  4:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-21  3:25 [Caml-list] Random results markus.kliegl
2001-04-21  4:31 ` Patrick M Doane
     [not found] <20010420211305.A7171@opus.davidb.org>
2001-04-21  4:33 ` markus.kliegl

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).