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.

- 95 -LIST_ERRORThe test for an empty list checks for a list with an error object in the head:def isnil L =if islist Lthen iserror (HEAD L)else falsedef ISNIL L =if islist Lthen MAKE_BOOL (iserror (HEAD L))else LIST_ERRORWe will now define a variety of elementary operations on lists.6.4. Linear length of a listA list is a sequence of an arbitrary number of objects. To find out how many objects are in a linear list: if the list isempty then there are 0 objects:LENGTH NIL = 0and otherwise there are 1 more than the number in the tail:LENGTH (CONS H T) = SUCC (LENGTH T)For example:LENGTH (CONS 1 (CONS 2 (CONS 3 NIL))) -> ... ->SUCC (LENGTH (CONS 2 (CONS 3 NIL))) -> ... ->SUCC (SUCC (LENGTH (CONS 3 NIL))) -> ... ->SUCC (SUCC (SUCC (LENGTH NIL))) -> ... ->SUCC (SUCC (SUCC 0))) ==3In our notation, this is:rec LENGTH L =IF ISNIL LTHEN 0ELSE SUCC (LENGTH (TAIL L))For example, consider:LENGTH (CONS 1 (CONS 2 NIL)) -> ... ->SUCC (LENGTH (TAIL (CONS 1 (CONS 2 NIL)))) -> ... ->SUCC (LENGTH (CONS 2 NIL)) -> ... ->SUCC (SUCC (LENGTH (TAIL (CONS 2 NIL)))) -> ... ->

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

Saved successfully!

Ooh no, something went wrong!