### rearrange lecture 10 to do currying first

parent b7351597
 ... ... @@ -3,54 +3,6 @@ let _ = UTop.set_show_box false *) (* function composition (right-to-left, like math) *) let compose = fun f -> fun g -> fun x -> f (g x) (* infix notation for composition *) let (%) = compose let double_plus_one x = compose (fun x -> x + 1) (fun x -> 2 * x) x let _ = double_plus_one 3 (* "variable-style" let binding is better style *) let double_plus_one = compose (fun x -> x + 1) (fun x -> 2 * x) let _ = double_plus_one 3 (* also nice to use the infix operator *) let double_plus_one = (fun x -> x + 1) % (fun x -> 2 * x) let _ = double_plus_one 3 let sqrt_of_abs i = Float.sqrt (float_of_int (Int.abs i)) let sqrt_of_abs i = (Float.sqrt % float_of_int % Int.abs) i let sqrt_of_abs = Float.sqrt % float_of_int % Int.abs let _ = sqrt_of_abs (-8) (* pipeline operator (left-to-right) *) (* actually built-in to OCaml, but here's the definition *) let (|>) x f = f x let sqrt_of_abs i = i |> Int.abs |> float_of_int |> Float.sqrt let _ = sqrt_of_abs (-8) (* also left-to-right *) let pipeline_option (f, g) = fun x -> match f x with | None -> None | Some y -> g y let sqrt_if_positive = pipeline_option ((fun i -> if i > 0 then Some (float_of_int i) else None), (fun x -> Some x) % sqrt) let _ = sqrt_if_positive 3 let _ = sqrt_if_positive (-3) (* ---------------------------------------- *) (* Currying *) let sorted3_tuple (x, y, z) = ... ... @@ -108,16 +60,62 @@ let forall p = List.fold_left (fun acc x -> acc && p x) true let has_zero = exists (fun x -> x = 0) let no_zeros = forall (fun x -> x <> 0) (* currying is the norm in OCaml, even if partial application unlikely *) let rec append xs ys = match xs with | [] -> ys | x::xs' -> x :: append xs' ys (* currying is the norm in OCaml, even if partial application is unlikely *) let rec append xs ys = match xs with | [] -> ys | x::xs' -> x :: append xs' ys (* revisit compose *) (* next idiom: combining functions *) (* function composition (right-to-left, like math) *) let compose = fun f -> fun g -> fun x -> f (g x) let compose_shorter f g x = f (g x) (* infix notation for composition *) let (%) = compose let double_plus_one x = compose (fun x -> x + 1) (fun x -> 2 * x) x let _ = double_plus_one 3 (* "variable-style" let binding is better style *) let double_plus_one = compose (fun x -> x + 1) (fun x -> 2 * x) let _ = double_plus_one 3 (* also nice to use the infix operator *) let double_plus_one = (fun x -> x + 1) % (fun x -> 2 * x) let _ = double_plus_one 3 let sqrt_of_abs i = Float.sqrt (float_of_int (Int.abs i)) let sqrt_of_abs i = (Float.sqrt % float_of_int % Int.abs) i let sqrt_of_abs = Float.sqrt % float_of_int % Int.abs let _ = sqrt_of_abs (-8) (* pipeline operator (left-to-right) *) (* actually built-in to OCaml, but here's the definition *) let (|>) x f = f x let sqrt_of_abs i = i |> Int.abs |> float_of_int |> Float.sqrt let _ = sqrt_of_abs (-8) (* also left-to-right *) let pipeline_option f g = fun x -> match f x with | None -> None | Some y -> g y let sqrt_if_positive = pipeline_option (fun i -> if i > 0 then Some (float_of_int i) else None) ((fun x -> Some x) % sqrt) let _ = sqrt_if_positive 3 let _ = sqrt_if_positive (-3) (* converting between curried and tupled versions of a function *) let curried_of_paired f x y = f (x, y) let paired_of_curried f (x, y) = f x y ... ... @@ -157,7 +155,7 @@ let _ = remove_empty_lists [[]; [true]] *) (* workaround 1: go back to bad style :\ *) (* workaround if you want polymrophics: go back to "bad" style :\ *) let remove_empty_lists ls = List.filter (fun x -> List.length x > 0) ls let _ = remove_empty_lists [[]; ] let _ = remove_empty_lists [[]; [true]] ... ...
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment