Ok, right off I find an intro text from my alma-matter (Caltech), but I'm starting to wonder about its assembly. Here's one of the first examples of a routine/function they give:
let rec gcd a b = let r = a mod b in if r = 0 then b else gcd b rOh bother, tail recursion. Fine, lets try a different example:
let rec fact n = if n = 1 then 1 else n * fact n - 1# fact(10) ;;
Hmmm. Maybe we better read more of the manual. Ok, so a function is supposed to look like this:let dbl = fun n -> n * 2 ;;
Hmmm. Now I'm completely confused. Those don't look anything like each other. Lets keep reading and see if it gets any clearer:
let sum = fun i j -> i + j;;
let sum = (fun i -> (fun j -> i + j));; (* same thing *)
Oh that was pretty scary, a function that takes one parameter and then calls another function (which is defined inline) which eats the next parameter. This is getting worse. Ok, lets look at our factorial program again. Maybe we can debug it:
let rec factp n = Printf.printf "fact %d\n" n if n = 1 then 1 else n * fact n - 1Well that won't even compile. Looks like the printf needs some sort of terminator or something. This is starting to look fishy. Ok, maybe its because its not a function (one can always hope). Taking a look at the last line though, I'm starting to suspect that the order of operations is
(fact n) -1
not fact (n-1)
. This is why I almost always put the parenthesis in when writing expressions in C. Ok, try it with parenthesis's.
# let rec fact n = if n = 1 then 1 else n * fact (n - 1) ;; val fact : int -> int =Ok, it works for small numbers at least. I guess its overflowing an int. Well, I guess that's ok. I think Lua throws in free bignum support, but this is actually typed as an int function, in fact trying to call it with a float produces an error. (You can't even use + or - with floats, you have to use type safe operators: +. and -. )# fact 10 ;; - : int = 3628800 # fact 100 ;; - : int = 0
Feb '04
Oops I dropped by satellite.
New Jets create excitement in the air.
The audience is not listening.
Mar '04
Neat chemicals you don't want to mess with.
The Lack of Practise Effect
Apr '04
Scramjets take to the air
Doing dangerous things in the fire.
The Real Way to get a job
May '04
Checking out cool tools (with the kids)
A master geek (Ink Tank flashback)
How to play with your kids