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.

- 99 -For example:1::(2::(3::(4::[]))) == 1::2::3::4::[] == [1,2,3,4]6.7. Lists and evaluationIt is important <strong>to</strong> note that we have adopted tacitly a weaker form of β reduction with lists because we are notevaluating fully the expressions corresponding <strong>to</strong> list representations. A list of the form:::is shorthand for:CONS which is a function application and should, strictly speaking, be evaluated.Here, we have tended <strong>to</strong> use a modified form of applicative order where we evaluate the arguments and <strong>to</strong> get values and but do not then evaluate theresulting function application:any further.CONS Similarly, a list of the form:[,]has been evaluated <strong>to</strong>:[,]but no further even though it is equivalent <strong>to</strong>:CONS (CONS NIL)This should be born in brain until we discuss evaluation in more detail in chapter 9.6.8. Deletion from a listTo add a new value <strong>to</strong> an unordered list we CONS it on the front. To delete a value, we must then search the list untilwe find it. Thus, if the list is empty then the value is not in it so return the empty list:DELETE X [] = []It is common in list processing <strong>to</strong> return the empty list if list access fails.Otherwise, if the first value in the list is the required value then return the rest of the list:DELETE X (H::T) = T if X HOtherwise, join the first value on<strong>to</strong> the result of deleting the required value from the rest of the list:DELETE X (H::T) = H::(DELETE X T) if NOT ( X H)

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

Saved successfully!

Ooh no, something went wrong!