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)
Arguments, Parameters Review notes from Mon., 5 Oct. on parameters, arguments, pass by value, pass by reference. Recall: Arguments Parameters ... int f(int x, int y, int z){ x = f(3,a,b+c); ... Arguments are also called “actual parameters” (as opposed to “formal parameters” in the function definition)
Arguments, Parameters Review notes from Mon., 5 Oct. on parameters, arguments, pass by value, pass by reference. Recall: Arguments Parameters ... int f(int x, int y, int z){ x = f(3,a,b+c); ... Arguments are also called “actual parameters” (as opposed to “formal parameters” in the function definition)
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
Examples of Function Calls (Python) >>> def f(a=1,b=1,c=1): ... return 100*a+10*b+c ... Default parameter values >>> f(10,20,30) 1230 >>> f(10) 1011 >>> f() 111
Examples of Function Calls (Python) >>> def f(a=1,b=1,c=1): ... return 100*a+10*b+c ... Named arguments >>> f(b=20) 301 >>> f(a=10,c=30) 1040 >>> f(c=30,b=20,a=10) 1230
Examples of Function Calls (Haskell) Prelude> let f a b c = 100 * a + 10 * b + c Prelude> f 10 20 30 g is a function obtained by 1230 giving parameter “a” the Prelude> let g = f 1 value “1” in function f. This Prelude> g 20 30 is called “currying”. 330 More currying; h x is Prelude> let h = g 1 equivalent to g 1 x, which Prelude> h 30 is equivalent to f 1 1 x 140
Parameter Evaluation (Ch. 6, p. 275) “Applicative Order” evaluation: arguments evaluated before the function call. (“Eager” evaluation.) int main(){ int slow(int n){ int x; .../* count to n^2 */ x = f(10,slow(1000000)); return count; printf(“%d\n”,x); } } time ./a.out int f(int a, int b){ a = 11 real 0m20.131s return a+1; user 0m20.126s } sys 0m0.000s
Parameter Evaluation (Ch. 6, p. 275) “Normal Order” evaluation: arguments are not evaluated until they are needed (possibly never): File lazy.hs: In ghci, try: Prelude> :l lazy slow 0 = 0 [1 of 1] Compiling Main ... Ok, modules loaded: Main. slow n = 1+slow (n-1) *Main> f 10 (slow 10000000) 11 *Main> f (slow 10000000) 10 f a b = a + 1 10000001
Parameter Evaluation “Lazy” evaluation: arguments are evaluated at most once (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).
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.
Recommend
More recommend