SLIDE 1 Weds., 28 Oct. 2015
Thank you! Your support of Gator Day was
- wonderful. You can still fill in the interest form:
http://tinyurl.com/ac-summer-success
Please respond to the survey! Today: More on functions Friday: Keep reading chapter 8 Next Weds.: 2nd exam (review Monday)
SLIDE 2 Arguments, Parameters
Review notes from Mon., 5 Oct. on parameters, arguments, pass by value, pass by reference. Recall:
... x = f(3,a,b+c); Arguments are also called “actual parameters” (as
- pposed to “formal parameters” in the function definition)
int f(int x, int y, int z){ ... Arguments Parameters
SLIDE 3 Arguments, Parameters
Review notes from Mon., 5 Oct. on parameters, arguments, pass by value, pass by reference. Recall:
... x = f(3,a,b+c); Arguments are also called “actual parameters” (as
- pposed to “formal parameters” in the function definition)
int f(int x, int y, int z){ ... Arguments Parameters
SLIDE 4
Examples of Function Calls (Python)
>>> def f(a,b,c): ... return 100*a+10*b+c ... >>> f(10,20,30) 1230 >>> f(10) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes exactly 3 arguments
SLIDE 5
Examples of Function Calls (Python)
>>> def f(a=1,b=1,c=1): ... return 100*a+10*b+c ... >>> f(10,20,30) 1230 >>> f(10) 1011 >>> f() 111 Default parameter values
SLIDE 6
Examples of Function Calls (Python)
>>> def f(a=1,b=1,c=1): ... return 100*a+10*b+c ... >>> f(b=20) 301 >>> f(a=10,c=30) 1040 >>> f(c=30,b=20,a=10) 1230 Named arguments
SLIDE 7
Examples of Function Calls (Haskell)
Prelude> let f a b c = 100 * a + 10 * b + c Prelude> f 10 20 30 1230 Prelude> let g = f 1 Prelude> g 20 30 330 Prelude> let h = g 1 Prelude> h 30 140
g is a function obtained by giving parameter “a” the value “1” in function f. This is called “currying”. More currying; h x is equivalent to g 1 x, which is equivalent to f 1 1 x
SLIDE 8
Parameter Evaluation (Ch. 6, p. 275)
“Applicative Order” evaluation: arguments evaluated before the function call. (“Eager” evaluation.)
int slow(int n){ .../* count to n^2 */ return count; } int f(int a, int b){ return a+1; } int main(){ int x; x = f(10,slow(1000000)); printf(“%d\n”,x); }
time ./a.out a = 11 real 0m20.131s user 0m20.126s sys 0m0.000s
SLIDE 9
Parameter Evaluation (Ch. 6, p. 275)
“Normal Order” evaluation: arguments are not evaluated until they are needed (possibly never):
File lazy.hs: slow 0 = 0 slow n = 1+slow (n-1) f a b = a + 1
In ghci, try: Prelude> :l lazy [1 of 1] Compiling Main ... Ok, modules loaded: Main. *Main> f 10 (slow 10000000) 11 *Main> f (slow 10000000) 10 10000001
SLIDE 10 Parameter Evaluation
“Lazy” evaluation: arguments are evaluated at most
- nce (possibly never). Even though we used a Haskell
example to illustrate “normal order”, it is more accurate to call Haskell’s evaluation order “lazy.” In normal order, a parameter could be evaluated more than once (i.e., evaluated each time it appears in the function).
SLIDE 11 In-class exercise
In groups of 3 or 4, put together a list of most confusing/least understood topics covered so far in the course. Try to be specific (e.g., “confused about types” is vague; “confused about union and record types” is more precise; “confused about union example in
- Oct. 14 notes” is very precise). Elect one
member to report them in a Slack post by the end of class today.