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.

- 126 -[]for matching against the empty list, and of the form:(H::T)so that H matches the head of a list argument and T matches the tail. We will now allow arbitrary bound variable listsfor matching against arbitrary list arguments. The bound variable lists may contain implicit or explicit empty lists formatching against empty lists in arguments.For example, we can use structure matching <strong>to</strong> redefine the circulation list selec<strong>to</strong>r functions:def FORENAME [F,S] = Fdef SURNAME [F,S] = SHere the bound variable list:[F,S] == F::S::NILmatches the argument list:so:[,] == ::::NILF == S == We can also pick up the forename and surname from the first entry in a list of names by structuring matching with thebound variable list:([F,S]::T)so [F,S] matches the head of the list and T matches the tail. For example we might count how often a givenforename occurs in a circulation list. If the list is empty the the count is 0. If the list is not empty then if the forenamematches that for the first entry then add 1 <strong>to</strong> the count for the rest of the list. Otherwise, return the count for the rest ofthe list:rec NCOUNT N [] = 0or NCOUNT N ([F,S]::T) =IF EQUAL N FTHEN 1 + (NCOUNT N T)ELSE (NCOUNT N T)For example, we can redefine the s<strong>to</strong>ck control selec<strong>to</strong>r functions as:def ITEM [I,S,R] = Idef STOCK [I,S,R] = Sdef REORDER [I,S,R] = RHere, the bound variable list:[I,S,R] == I::S::R::NIL

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

Saved successfully!

Ooh no, something went wrong!