10.07.2015 Views

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

An Introduction to Functional Programming Through Lambda Calculus

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

- 177 -- fun length [] = 0 |length (h::t) = 1+(length t);> val length = fn : (’a list) -> intThere are no explicit types in the pattern:(h::t)and this pattern is consistent if h is ’a and t is ’a list as (h::t) is then ’a list. This is also consistent withthe use of t as the argument in the recursive call <strong>to</strong> length. Here again, the element types are irrelevant for purelystructural manipulations.This approach can also be used <strong>to</strong> define type independent functions which are later made type specific.For example, we might try and define a general purpose list insertion function as:- fun insert i [] = [i] |insert i (h::t) =if i bool) -> (’a -> ((’a list) -> (’a list)))Here, comp needs an (’a * ’a) argument <strong>to</strong> be consistent in insert and must return a bool <strong>to</strong> satisfy its use inthe if.Now, different typed comparison functions may be used <strong>to</strong> construct different typed insertion functions. For example,we could construct a string insertion function through partial application by passing a string comparison function:fn (s1:string,s2:string) => s1 s1 val sinsert = fn : string -> ((string list) -> (string list))Here, the comparison function is:(string * string) -> boolso ’a must be string in the rest of the function’s type.For example, we could also construct a integer insertion function through partial application by passing an integercomparison function:fn (i1:integer,i2:integer) => i1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!