lec03 - (* binding - reminder about let and letrec *) let...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
(* binding - reminder about let and letrec *) let foo(x) = x+1 in let bar(x) = foo(x-1) in bar(3110) let foo(x) = x+1 and bar(x) = foo(x-1) (* unbound *) in bar(3110) let rec foo(x) = x+1 and bar(x) = foo(x-1) in bar(3110) (* functions foo and bar can refer to themselves and each other *) let rec x=1 and y = x+1 in x+y (* example: computing square roots *) let squareRoot(x) = let rec (* numerical accuracy *) delta = 1e-12 (* returns true iff the guess is good enough *) and goodEnough(guess) = abs_float(guess *. guess -. x) < delta (* return a better guess by averaging it with x/guess *) and improve(guess) = (guess +. x /. guess) /. 2.0 (* Return the square root of x, starting from an initial guess. *) and tryGuess(guess) = if goodEnough(guess) then guess else tryGuess(improve(guess)) in (* start with a guess of 1.0 *) tryGuess(1.0) let square (x) = x *. x (* form of function definitions we have been using, float * float -> float *) let sum(x,y) = x +. 2. *. y (* shorthand for pattern matching of tuple, as seen in recitation *)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/25/2009 for the course PHYS 2214 at Cornell.

Page1 / 3

lec03 - (* binding - reminder about let and letrec *) let...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online