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.

- 90 -Next, we will develop elementary functions for manipulating linear lists and simpler list notations. We will alsointroduce strings as lists of characters.We will then introduce structure matching for list functions and look at a variety of operations on linear lists. Inimperative languages, we might use arrays in these applications.Finally, we will consider at the use of mapping functions <strong>to</strong> generalise operations on linear lists.6.2. ListsLists are general purpose data structures which are widely used within functional and logic programming. They werebrought <strong>to</strong> prominence through LISP and are now found in various forms in many languages. Lists may be used inplace of both arrays and record structures, for example <strong>to</strong> build stacks, queues and tree structures.In this chapter, we are going <strong>to</strong> introduce lists in<strong>to</strong> our functional notation and look at using lists in problems wherearrays might be used in other languages. In chapter 7 we will look at using lists where record structures might be usedin other languages.Lists are variable length sequences of objects. A strict approach <strong>to</strong> lists, as in ML for example, treats them as variablelength sequences of objects of the same type. We will take the relatively lax approach of LISP and Prolog and treatthem as variable length sequences of objects of mixed type. Although this is less rigorous theoretically and makes aformal treatment more complex it simplifies presentation and provides a more flexible structure.Formally, a list is either empty, denoted by the unique object:NIL is a lis<strong>to</strong>r it is a constructed pair with a head which is any object and a tail which is a list:CONS H T is a listif H is any object and T is a listCONS is the traditional name for the list construc<strong>to</strong>r, originally from LISP.For example, from the object 3 and the list NIL we can construct the list:CONS 3 NILwith 3 in the head and NIL in the tail.From the object 2 and this list we can construct the list:CONS 2 (CONS 3 NIL)with 1 in the head and the list CONS 3 NIL in the tail.From the object 1 and the previous list we can construct the list:CONS1 (CONS 2 (CONS 3 NIL))with 1 in the head and the list CONS 2 (CONS 3 NIL)) in the tail, and so on.Note that the tail of a list must be a list. Thus, all lists end, eventually, with the empty list.Note that the head of a list may be any object including another list, enabling the construction of nested structures,particularly trees.

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

Saved successfully!

Ooh no, something went wrong!