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.

- 127 -matches the argument:so:[,,] ==::::::NILI == S == R == We can use the bound variable list:([I,S,R]::T)<strong>to</strong> match against a s<strong>to</strong>ck control list so that [I,S,R] matches the first item and T matches the rest of the list. Forexample, we might find all the items that need <strong>to</strong> be reordered. If the list is empty then return the empty list.If the listis not empty, if the first item needs <strong>to</strong> be reordered then add it <strong>to</strong> those <strong>to</strong> be reordered in the rest of the list. Otherwisereturn those <strong>to</strong> be reordered in the rest of the list:rec REORD [] = 0or REORD ([I,S,R]::T) =IF LESS S RTHEN [I,S,R]::(REORD T)ELSE REORD TFor example, we can redefine the telephone direc<strong>to</strong>ry selec<strong>to</strong>r functions as:def ENAME [N,A,P] = Ndef EFORENAME [[F,S],A,P] = Fdef ESURNAME [[F,S],A,P] = Sdef ADDRESS [N,A,P] = Adef PHONE [N,A,P] = PHere, the bound variable list:[N,A,P] == N::A::P::NILmatches the argument list:so:[,,] == ::::::NILN == A == P == Similarly, the bound variable list:[[F,S],A,P] == (F::S::NIL)::A::P::NILmatches the argument list:

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

Saved successfully!

Ooh no, something went wrong!