80 likes | 234 Views
CS 341 Programming Language Design and Implementation. Administrative HW #6 is released — due Monday 3/3 @ 11am (class start) image processing in F# Today? Winding down our discussion of functional programming Anyone for curry? most recent HW and Quiz. Function parameters == tuples
E N D
CS 341 Programming Language Design and Implementation • Administrative • HW #6 is released — due Monday 3/3 @ 11am (class start) • image processing in F# • Today? • Winding down our discussion of functional programming • Anyone for curry? • most recent HW and Quiz CS 341 -- 21 Feb 2014
Function parameters == tuples • parameters in F# are not what you think… Not 2 parameters, but 1 tuple let add2(x, y) = x + y let result = add2(10, 20) printf"add2: %A\n" add2(10, 20) let add2 (x, y) = x + y let result = add2 (10, 20) printf"add2: %A\n" (add2(10, 20)) X Error: compiler sees 3 args to printf Correct: 2 argsto printf CS 341 -- 21 Feb 2014
Declaring functions in F# • Two ways to declare — and the calls must match! • tupled: more traditional syntax with ( ) • curried: more flexible approach without ( ) letadd2(x, y) = x + y // tupled: val add2 : int * int -> int letR1 = add2(10, 20) // call with ( ) printfn "%A" R1 // 30 let add2cx y = x + y // curried: val add2c : int -> int -> int let R2 = add2c 10 20 // call without ( ) printfn "%A" R2 // 30 CS 341 -- 21 Feb 2014
Def: a function is curried if you can supply fewer arguments — in which case the function is not called, but a new function is returned instead. Curried? • What does that mean? • And more importantly, why? • more flexible — allows you to use functions in situations where 1 or more arguments are supplied later… let add x y = x + y let R = add 10 20 let incr= add 1 add: int-> int -> int R: int incr: int -> int CS 341 -- 21 Feb 2014
Real example? • Computing standard deviation… part of standard deviation computation let DiffSquared m x = Math.Pow(x-m, 2.0) let stddev(values) = let N = length(values) let mean = sum(values) / float(N) let diffs = List.map (DiffSquared mean) values Math.Sqrt( sum(diffs) / float(N) ) CS 341 -- 21 Feb 2014
F# convention: • Most functions are defined in curried form without ( ) • and so we don't use ( ) when we call them… let add x y = x + y let R1 = add 10 20 let L2 = List.map (add 1) L printfn"%A, %A" R1 L2 CS 341 -- 21 Feb 2014
Homework 5 • Lots of ways to solve, I used 4 lists -> 2 lists -> 1 list 100 60 90 . . [100; 60; 90; …] 90 80 90 . . [90; 80; 90; …] [ weighted scores ] 22 60 90 . . [22; 60; 90; …] [ final scores ] 100 100 100 . . [22; 60; 90; …] [ weighted scores ] CS 341 -- 21 Feb 2014
Quiz 2 • Worse than expected… • Average: 66 • High: 100 • Low: 5 CS 341 -- 21 Feb 2014