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.

- 109 -In general, for:rec =IF ISNIL THEN ELSE we will write:rec [] = or (::) = where and are bound variables.Consider the definition of the linear length of a list:LENGTH [] = 0LENGTH (H::T) = SUCC (LENGTH T)which we wrote as:rec LENGTH L =IF ISNIL LTHEN 0ELSE SUCC (LENGTH (TAIL L))We will now write:rec LENGTH [] = 0or LENGTH (H::T) = SUCC (LENGTH T)For example, suppose we have a list made up of arbitrarily nested lists which we want <strong>to</strong> flatten in<strong>to</strong> one long linearlist, for example:FLAT [[1,2,3],[[4,5],[6,7,[8.9]]]] => ... =>[1,2,3,4,5,6,7,8,9]The empty list is already flat:FLAT [] = []If the list is not empty then if the head is not a list then join it <strong>to</strong> the flattened tail:FLAT (H::T) = H::(FLAT T) if NOT (ISLIST H)Otherwise, append the flattened head <strong>to</strong> the flattened tail:FLAT (H::T) = APPEND (FLAT H) (FLAT T) if ISLIST HIn our old notation we would write:rec FLAT L =IF ISNIL LTHEN []ELSE

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

Saved successfully!

Ooh no, something went wrong!