View Full Version : question regarding "official" examples
I'm reading the 100 lines of O'caml (http://caml.inria.fr/FAQ/exemples-eng.html) examples, and I'm getting syntax errors in the insertion sort example, even when I copy it directly from the page. Wondering if anyone knows what's up....
here's the code and error from the interpreter:
# let rec sort = function
| [] -> []
| x :: l -> insert x (sort l)
and insert elem = function
| [] -> [elem]
| x :: l -> if elem < x then elem :: x :: l else x :: insert elem l;;
Characters 1-2:
| x :: l -> if elem < x then elem :: x :: l else x :: insert elem l;;
^
Syntax error
Thanks for any help.
PrBacterio
09-06-2002, 03:40 PM
I suppose you are using the windows GUI version of the interpreter. That is a problem with the readline function it uses; you cant copy/paste multiple lines into it, it will only 'see' the last line. When you enter it by hand, or copy it line by line, it should work. Or simply put it into a file and load that. Or use the command-line interpreter. Or ... :)
PrBacterio: Thanks. I just found that out and was coming back to mention that. :) I'll just use the command line version. :) (Another problem with the gui is that when you open the help->about dialog, there's no way to close it!) ..
Hmm. okay, apparently O'caml doesn't like me very much.. I'm trying to use the toplevel directive "trace", and I get ...
# let rec fib x = if x <= 1 then 1 else fib (x - 1) + fib (x - 2);;
val fib : int -> int = <fun>
# fib 8
;;
- : int = 34
# fib 5;;
- : int = 8
# trace "fib";;
Characters 0-5:
trace "fib";;
^^^^^
Unbound value trace
is it me? or did I happen to run into something else?
GnuVince
09-06-2002, 05:45 PM
I think trace is only in Caml Light. I once tried it in O'Caml and it didn't work :-/
PrBacterio
09-06-2002, 07:00 PM
As trace is a directive, you need to prepend it with a # sign, as with all directives (#use, #line, ...):
# let rec fact n = if n < 2 then 1 else n * fact (n - 1);;
val fact : int -> int = <fun>
# #trace fact;;
fact is now traced.
# fact 3;;
fact <-- 3
fact <-- 2
fact <-- 1
fact --> 1
fact --> 2
fact --> 6
- : int = 6
GnuVince
09-06-2002, 07:48 PM
Cool! Thanks PrBacterio!
PrBacterio, Thanks. :) Heh. See, I was reading the examples, and I saw the # in front of the function definitions, and I wondered what it could mean.. Then I got the interpreter and I realized it was the prompt. So when I saw the # in front of the 'trace' I assumed it was the prompt. Thanks again for clearing that up! Hrmm. I might just learn this damnable language yet!
GnuVince
09-07-2002, 01:33 AM
kmj: O'caml gives great enlightenment! At least, it gave me some ;)
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.