UA CSC 520 - Study Notes (10 pages)

Previewing pages 1, 2, 3 of 10 page document View the full content.
View Full Document

Study Notes



Previewing pages 1, 2, 3 of actual document.

View the full content.
View Full Document
View Full Document

Study Notes

72 views

Lecture Notes


Pages:
10
School:
University of Arizona
Course:
Csc 520 - Principles of Programming Languages
Principles of Programming Languages Documents

Unformatted text preview:

CSc 520 Principles of Programming Languages 16 Haskell Higher Order Functions Christian Collberg Department of Computer Science University of Arizona collberg cs arizona edu Copyright c 2005 Christian Collberg April 22 2005 1 Higher Order Functions A function is Higher Order if it takes a function as an argument or returns one as its result Higher order function aren t weird the differentiation operation from high school calculus is higherorder deriv Float Float Float Float deriv f x f x dx f x 0 0001 Many recursive functions share a similar structure We can capture such recursive patterns in a higher order function We can often avoid the use of explicit recursion by using higher order functions This leads to functions that are shorter and easier to read and maintain 2 Currying Revisited We have already seen a number of higher order functions In fact any curried function is higher order Why Well when a curried function is applied to one of its arguments it returns a new function as the result Uh what was this currying thing A curried function does not have to be applied to all its arguments at once We can supply some of the arguments thereby creating a new specialized function This function can for example be passed as argument to a higher order function 1 3 Currying Revisited How is a curried function defined A curried function of n arguments of types t1 t2 tn that returns a value of type t is defined like this fun t1 t2 tn t This is sort of like defining n different functions one for each In fact we could define these functions explicitly but that would be tedious fun1 t2 tn t fun1 a2 an fun2 t3 tn t fun2 a3 an 4 Currying Revisited Duh how about an example Certainly Lets define a recursive function get nth n xs which returns the n th element from the list xs get nth 1 x x get nth n xs get nth n 1 xs get nth 10 Bartholomew e Now let s use get nth to define functions get second get third get fourth and get fifth without using explicit recursion get second get nth 2



View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?