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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

- 125 -["VDU",25,12]::["modem",20,12]::[["printer",125,10]] ==[["VDU",25,12],["modem",20,12]["printer",125,10]]["printer",125,10]]) -> ... ->For example, given a telephone direc<strong>to</strong>ry, we might want <strong>to</strong> add a new entry in alphabetic surname order. If thedirec<strong>to</strong>ry is empty then make a new direc<strong>to</strong>ry from the new entry. If the direc<strong>to</strong>ry is not empty then if the new entrycomes before the first entry then add it <strong>to</strong> the front of the direc<strong>to</strong>ry. Otherwise, add the first entry <strong>to</strong> the result ofadding the new entry <strong>to</strong> the rest of the direc<strong>to</strong>ry:rec DINSERT E [] = [E]or DINSERT E (H::T) =IF STRING_LESS (ESURNAME E) (ESURNAME H)THEN E::H::TELSE H::(DINSERT E T)For example:DINSERT[["Chris","Catnip"],"Credit",3333][[["<strong>An</strong>na","Able"],"Accounts",1212],[["Betty","Baker"],"Boiler room",4242],[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",2424]] -> ... ->[["<strong>An</strong>na","Able"],"Accounts",1212]::(DINSERT[["Chris","Catnip"],"Credit",3333][[["Betty","Baker"],"Boiler room",4242],[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",2424]]) -> ... ->[["<strong>An</strong>na","Able"],"Accounts",1212]::[["Betty","Baker"],"Boiler room",4242]::(DINSERT[["Chris","Catnip"],"Credit",3333][[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",2424]]) -> ... ->[["<strong>An</strong>na","Able"],"Accounts",1212]::[["Betty","Baker"],"Boiler room",4242]::[["Chris","Catnip"],"Credit",3333]::[[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",2424]] ==[[["<strong>An</strong>na","Able"],"Accounts",1212],[["Betty","Baker"],"Boiler room",4242],[["Chris","Catnip"],"Credit",3333],[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",2424]]7.5. Generalised structure matchingIn chapter 6 we introduced structure matching in<strong>to</strong> function definitions. Objects are defined in terms of constant basecases and structured recursion cases. Thus, function definitions have base cases with constants instead of boundvariables for matching against constant arguments, and recursion cases with structured bound variable for matchingagainst structured arguments. In particular, for list processing we have used bound variable lists of the form:

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

Saved successfully!

Ooh no, something went wrong!