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.

- 123 -For example:IF STRING_EQUAL S (HEAD (TAIL (HEAD H)))THEN [(HEAD H),(HEAD TAIL H),N]::TELSE H::(DCHANGE S N T)DCHANGE "Charlie" 2424[[["<strong>An</strong>na","Able"],"Accounts",1212],[["Betty","Baker"],"Boiler room",4242],[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",1234]] => ... =>[["<strong>An</strong>na","Able"],"Accounts",1212]::(DCHANGE "Charlie" 2424[[["Betty","Baker"],"Boiler room",4242],[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",1234]]) => ... =>[["<strong>An</strong>na","Able"],"Accounts",1212]::[["Betty","Baker"],"Boiler room",4242]::(DCHANGE "Charlie" 2424[[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",1234]]) => ... =>[["<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],[["Betty","Baker"],"Boiler room",4242],[["Clarice","Charlie"],"Cus<strong>to</strong>mer orders",2424]]7.4. Selec<strong>to</strong>r functionsBecause composite values are being represented by lists, the above examples all depend on the nested use of the listselec<strong>to</strong>rs HEAD and TAIL <strong>to</strong> select sub-values from composite values. For composite values with many sub-values,this list selection becomes somewhat dense. Instead, we might define selec<strong>to</strong>r functions which are named <strong>to</strong> reflect thecomposite values that they operate on. These are particularly useful in LISP <strong>to</strong> simplify complex list expressions,because it lacks structure matching.For example, for names we might define:def FORENAME N = HEAD Ndef SURNAME N = HEAD (TAIL N)For example, for s<strong>to</strong>ck items we might define:def ITEM N = HEAD Ndef STOCK N = HEAD (TAIL N)def REORDER N = HEAD (TAIL (TAIL N))For example, for telephone direc<strong>to</strong>ry entries we might define:def NAME E = HEAD Edef EFORENAME E = FORENAME (NAME E)

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

Saved successfully!

Ooh no, something went wrong!