# lec02 - (* return a better guess by averaging it with...

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

(* tuples *) (3, "hi", true) (* let *) let z = 3 let f(x,y) = (x = String.length(y)) f(3,"foo") (* anonymous and higher order functions *) let square(x) = x*x let square = fun (x) -> x*x let twice(f,x):int = f(f(x)) twice(square,3) twice((fun(x) -> x*x), 3) (* higher order functions - values *) let twice2(f):(int -> int) = fun(x) -> f(f(x)) let fourth = twice2(square) let fourth = twice2(fun(x)->x*x) fourth(3) (* let rec and embedded lets *) let isPrime(n) = let rec noDivisorsAbove(m) = in if (n<1) then false else noDivisorsAbove(2) (* explicitly typed version*) let isPrime(n: int): bool = let rec noDivisorsAbove(m: int): bool = in if (n<1) then false else noDivisorsAbove(2) (* computing square roots *) let squareRoot(x) = let rec (* numerical accuracy *) delta = 1e-10

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

View Full Document
(* returns true iff the guess is good enough *) and goodEnough(guess) = abs_float(guess *. guess -. x) < delta
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (* 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) (* explicitly typed version *) let squareRoot(x: float): float = let rec (* numerical accuracy *) delta:float = 1e-10 (* returns true iff the guess is good enough *) and goodEnough(guess: float): bool = abs_float(guess *. guess -. x) < delta (* return a better guess by averaging it with x/guess *) and improve(guess: float): float = (guess +. x /. guess) /. 2.0 (* Return the square root of x, starting from an initial guess. *) and tryGuess(guess: float): float = if goodEnough(guess) then guess else tryGuess(improve(guess)) in (* start with a guess of 1.0 *) tryGuess(1.0)...
View Full Document

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

### Page1 / 2

lec02 - (* return a better guess by averaging it with...

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

View Full Document
Ask a homework question - tutors are online