# lec13 - (* pattern matching on lists *) ( let rec length =...

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (* pattern matching on lists *) ( let rec length = function -> 0 | _ :: t -> length (t)+1 let rec length l = match l with -> 0 | _ :: t -> length (t)+1 let rec nth lst n = match (lst, n) with (,_) -> raise(Failure "bad index") | (h::_,0) -> h | (_::t,_) -> nth t (n-1) (* pattern matching on trees *) ( type 'a tree = Node of 'a * 'a tree * 'a tree | Leaf t let rec depth = function Leaf -> 0 | Node(_, a, b) -> 1+max (depth a) (depth b) let rec contains x = function Leaf -> false | Node (y, l, r) -> if x=y then true else if x < y then contains x l else contains x r let rec contains x = function Leaf -> false | Node (y, l, r) -> x=y || (x < y && contains x l) || (x > y && contains x r) (* Familiarity with OCaml List module *) ( (* Higher order functions and currying *) ( let inc x = x+1 l let inc = function x -> x+1 l let twice f x = f(f(x)) let twice f = function x -> f(f(x)) twice inc 3 t twice (function(x) -> x+1) 3 t twice inc t twice twice inc 2 t (twice twice) inc 2 ( twice (twice inc) 2...
View Full Document

## This note was uploaded on 10/25/2009 for the course PHYS 2214 at Cornell University (Engineering School).

### Page1 / 3

lec13 - (* pattern matching on lists *) ( let rec length =...

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

View Full Document
Ask a homework question - tutors are online