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.

- 122 -For example:IF STRING_EQUAL S (HEAD (TAIL H))THEN HEAD HELSE NFIND S TNFIND "Charlie" [["<strong>An</strong>na","Able"],["Betty","Baker"],["Clarice","Charlie"]] -> ... ->NFIND "Charlie" [["Betty","Baker"],["Clarice","Charlie"]] -> ... ->NFIND "Charlie" [["Clarice","Charlie"]] -> ... ->"Clarice"For example, given a s<strong>to</strong>ck list, suppose we want <strong>to</strong> find all the items which have the s<strong>to</strong>ck level below the reorderlevel. If the list is empty then return the empty list. If the list is not empty then if the first item’s s<strong>to</strong>ck level is belowthe reorder level then add the first item <strong>to</strong> the result of checking the rest of the list. Otherwise, check the rest of thelist:rec SCHECK [] = []or SCHECK (H::T) =IF LESS (HEAD (TAIL H)) (HEAD (TAIL (TAIL H)))THEN H::(SCHECK T)ELSE SCHECK TFor example:SCHECK [["VDU",25,12],["modem",10,12],["printer",125,10],["mouse",7,12]] -> ... ->SCHECK [["modem",10,12],["printer",125,10],["mouse",7,12]] -> ... ->["modem",10,12]::(SCHECK [["printer",125,10],["mouse",7,12]]) -> ... ->["modem",10,12]::(SCHECK [["mouse",7,12]]) -> ... ->["modem",10,12]::["mouse",7,12]::(SCHECK []) -> ... ->["modem",10,12]::["mouse",7,12]::[] ==[["modem",10,12],["mouse",7,12]]For example, given a telephone direc<strong>to</strong>ry, suppose we want <strong>to</strong> change someone’s telephone number, knowing theirsurname. If the direc<strong>to</strong>ry is empty then return the empty list. If the direc<strong>to</strong>ry is not empty then if the required entry isthe first then add a modified first entry <strong>to</strong> the rest of the entries. Otherwise, add the first entry <strong>to</strong> the result of lookingfor the required entry in the rest of the entries:rec DCHANGE S N [] = []or DCHANGE S N (H::T) =

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

Saved successfully!

Ooh no, something went wrong!