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.

- 120 -3) i) Write a function which merges two ordered lists <strong>to</strong> produce an ordered list. Merging the empty list with anordered list gives that list. To merge two non-empty lists, if the head of the first comes before the head of thesecond then join the head of the first on<strong>to</strong> the result of merging the tail of the first and the second. Otherwise,join the head of the second on<strong>to</strong> the result of merging the first on<strong>to</strong> the tail of the second. For example:MERGE [1,4,7,9] [2,5,8] => ... =>[1,2,4,5,7,8,9]ii) Write a function which merges a list of ordered lists. For example:LMERGE [[1,4,7],[2,5,8],[3,6,9]] => ... =>[1,2,3,4,5,6,7,8,9]7. COMPOSITE VALUES AND TREES7.1. <strong>Introduction</strong>In this chapter we are going <strong>to</strong> discuss the use of composite values <strong>to</strong> hold records of related values.To begin with we will represent composite values as lists and process composite value sequences using linear listalgorithms.We will then introduce new notations <strong>to</strong> generalise list structure matching, <strong>to</strong> simplify list and composite valueprocessing.Finally, we will look at trees and consider the use of binary tree algorithms.7.2. Composite valuesSo far, we have been looking at processing sequences of single values held in lists. However, for many applications,the data is a sequence of composite values where each consists of a number of related sub-values. These sub-valuesmay in turn be composite so composite values may be nested.For example, in processing a circulation list, we need <strong>to</strong> know each person’s forename and surname. For example, inprocessing a s<strong>to</strong>ck control system, for each item in s<strong>to</strong>ck we need <strong>to</strong> know its name, the number in s<strong>to</strong>ck and the s<strong>to</strong>cklevel at which it should be reordered. For example, in processing a telephone direc<strong>to</strong>ry, we need <strong>to</strong> know eachperson’s name, address and telephone number. Here, the name might in turn consist of a forename and surname.Some languages provide special constructs for user defined composite values, for example the Pascal RECORD, the Cstructure and the ML tuple. These effectively add new types <strong>to</strong> the language.Here, we are going <strong>to</strong> use lists <strong>to</strong> represent composite values. This is formally less rigorous than introducing a specialconstruct but greatly simplifies presentation. We will look at the use of ML tuples in chapter 9.For example, we might represent a name consisting of a string and a string as the list:[,]for example:or["<strong>An</strong>na","Able"]

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

Saved successfully!

Ooh no, something went wrong!