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.

- 46 -Notice that iszero applies the number <strong>to</strong> the selec<strong>to</strong>r rather than the selec<strong>to</strong>r <strong>to</strong> the number. This is because ournumber representation models numbers as functions with selec<strong>to</strong>r arguments.We can now define the predecessor function pred so that:and so on.(pred one) => ... => zero(pred two) => ... => one(pred three) => ... => twoFor our number representation, pred should strip off a layer of nesting from an arbitrary number:λs.((s false) )and return the:This suggests using select_second because:(λs.((s false) ) select_second) =>((select_second false) ) ==((λfirst.λsecond.second false) ) =>(λsecond.second ) =>so we might define a first version of pred as:def pred1 = λn.(n select_second)However, there is a problem with zero as we only have positive integers. Let us try our present pred1 with zero:(pred1 zero) ==(λn.(n select_second) zero) =>(zero select_second) ==(λx.x select_second) =>select_second ==falsewhich is not a representation of a number.We could define the predecessor of zero <strong>to</strong> be zero and check numbers <strong>to</strong> see if they are zero before returningtheir predecessor, using: = zero ? zero : predecessor of

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

Saved successfully!

Ooh no, something went wrong!