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.

- 147 -IF ISZERO (ADD 1 2)THEN 1ELSE ADD (SUCC 1) (PRED (ADD 1 2)) => ... =>ADD (SUCC 1) (PRED (ADD 1 2)) => ... =>IF ISZERO (PRED (ADD 1 2))THEN SUCC 1ELSE ADD (SUCC (SUCC 1)) (PRED (PRED (ADD 1 2))) => ... =>ADD (SUCC (SUCC 1)) (PRED (PRED (ADD 1 2))) => ... =>IF ISZERO (PRED (PRED (ADD 1 2)))THEN SUCC (SUCC 1)ELSE ADD (SUCC (SUCC (SUCC 1))) (PRED (PRED (PRED (ADD 1 2)))) => ... =>ADD (SUCC (SUCC (SUCC 1))) (PRED (PRED (PRED (ADD 1 2)))) => ... =>IF ISZERO (PRED (PRED (PRED (ADD 1 2))))THEN SUCC (SUCC (SUCC 1))ELSE ... => ... =>SUCC (SUCC (SUCC 1)) ==4is returned.ADD 1 2 has been evaluated 4 times even though it only appeared once originally. In the initial call <strong>to</strong> ADD theargument ADD 1 2 is not evaluated because it is not a leftmost application. Instead it completely replaces the boundvariable Y throughout ADD’s body. Thereafter it is evaluated in the IF condition but when ADD is called recursively itbecomes an argument for the call so evaluation is again delayed. We also had <strong>to</strong> repeatedly evaluate otherapplications, for example PRED (ADD 1 2) in the condition, although we did not highlight these.In general, for normal order evaluation, an unevaluated application argument will replace all occurrences of theassociated bound variable in the function body. Each replacement is the site of initiation of potential additionalevaluation. Clearly, the more often the bound variable appears in the body, the more argument evaluation maymultiply.8.4. Applicative orderApplicative order β reduction of an application requires the evaluation ofs both the function and the argumentexpressions. More formally, this involves the evaluation of the left most redex free of internal redexes. For a functionapplication, this will result in each argument only being evaluated once.For example, evaluating our previous example in applicative order:ADD 1 (ADD 1 2) ->ADD 1 3 -> ... ->IF ISZERO 3THEN 1ELSE ADD (SUCC 1) (PRED 3) -> ... ->ADD (SUCC 1) (PRED 3) ->

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

Saved successfully!

Ooh no, something went wrong!