Commit fe0f3b65 authored by Dan Grossman's avatar Dan Grossman
Browse files

sec03 code, quite different from past due to Monday start -- only treatment of...

sec03 code, quite different from past due to Monday start -- only treatment of exceptions and map/filter use cannot yet use first-class functions
parent e7ffe55c
(* CSE 341, Section 03 *)
(* three parts:
1. Exceptions
2. Tail recursion
3. Using map and filter
*)
(* part 1: Exceptions (moved from Lecture 7) *)
exception MyUndesirableCondition
exception MyOtherException of int * int
let oh_no () =
raise MyUndesirableCondition
let oh_no_with_info () =
raise (MyOtherException (7, 42))
let catch_example () =
try oh_no ()
with MyUndesirableCondition -> 0
let catch_example_with_info () =
try oh_no_with_info ()
with MyOtherException (x, y) -> x + y
let boo () =
try oh_no ()
with MyOtherException (x, y) -> x + y
let hd xs =
match xs with
| [] -> raise MyUndesirableCondition
| x::_ -> x
let foo1 = hd [3;4;5]
(*let foo2 = hd []*)
let bar1 = try Some (hd [3;4;5]) with MyUndesirableCondition -> None
let bar2 = try Some (hd []) with MyUndesirableCondition -> None
let rec maxlist (xs,ex) = (* int list * exn -> int *)
match xs with
| [] -> raise ex
| x::[] -> x
| x::xs' -> let m = maxlist(xs',ex) in if x > m then x else m
let m1 = maxlist ([3;4;5],MyUndesirableCondition)
let m2 = try maxlist ([3;4;5],MyUndesirableCondition) with
MyUndesirableCondition -> 42
(*let m3 = maxlist ([],MyUndesirableCondition)*)
let m4 = try maxlist ([],MyUndesirableCondition) with
MyUndesirableCondition -> 42
(* part 2: tail-recursion practice *)
(* Example of a function already tail-recursive. *)
let rec second_to_last xs =
match xs with
| [] -> failwith "second_to_last: empty list"
| x::[] -> failwith "second_to_last: single-element list"
| x::_::[] -> x
| _::xs' -> second_to_last xs'
(* Example of making a function tail-recursive *)
let rec sum1 xs =
match xs with
| [] -> 0
| i::xs' -> i + sum1 xs'
let sum2 xs =
let rec f (xs,acc) =
match xs with
| [] -> acc
| i::xs' -> f (xs',i+acc) in
f (xs,0)
(* For you: count_empty_strings1 counts the number of empty strings in a list of strings *)
let rec count_empty_strings1 ss =
match ss with
| [] -> 0
| s::ss' -> (if s = "" then 1 else 0) + count_empty_strings1 ss'
(* TO DO: make count_empty_strings2 that is like count_empty_strings1 but tail recursive *)
(* tail recursion and list order *)
(* try to make this tail recursive -- how can you fix the order? *)
let rec every_other1 xs =
match xs with
| [] -> xs
| x::[] -> xs
| x::_::xs' -> x :: every_other1 xs'
(* part 3: practice using map and filter *)
(* two super-useful higher-order functions on lists
get very comfortable with what they compute and their types
*)
let rec map (f, xs) =
match xs with
| [] -> []
| x :: xs' -> (f x) :: (map (f, xs'))
let rec filter (f, xs) =
match xs with
| [] -> []
| x :: xs' -> if f x then
x :: filter (f,xs')
else
filter (f,xs')
(* using map and filter *)
(* try to implement each of these with 1-b2 lines by calling map or filter
(we will learn anonymous functions tomorrow, making them each 1 line instead of 2)
*)
(* A. Take a list of ints and return a new list with each int
divided by 2 -- example done for you
*)
let half_of_all xs =
let half_of i = i / 2 in
map (half_of, xs)
(* B. Take a list of floats and return a new float list with the
square root of the absolute value of each float.
(Hint: use Float.abs and sqrt) *)
let sqrts_of_abs_all fs =
let f x = sqrt (Float.abs x) in
map (f, fs)
(* C. Take a list of strings and return a new string list with
each string capitalized. (Hint: use String.capitalize_ascii) *)
let capitalize ss = (* notice no need for locally defined function! *)
map (String.capitalize_ascii, ss)
(* D. Take a list of pairs and return a list that contains
the first element of every pairs. *)
let firsts xs =
map (fst, xs)
(* E. Take a list of strings and return a string list that contains
only non-empty strings from the original list. *)
let no_empty_strings ss =
let not_empty s = s <> "" in
filter (not_empty, ss)
(* F. Take a list of ints and return an int list that contains
only even numbers from the original list. *)
let only_evens xs =
let is_even i = i mod 2 = 0 in
filter (is_even, xs)
(* G. Take a list of ints and return an int list that contains
only ints that are power of 2.
You can use the provided funtion is_power_of_two. *)
let rec is_power_of_two i =
i > 0 &&
(i=1 || (i mod 2 = 0 && is_power_of_two (i / 2)))
let only_powers_of_two xs =
filter (is_power_of_two, xs)
(* H. Take a list of ints and return an int list by
removing all the negative ints. *)
let remove_negatives1 xs =
let pos_or_zero i = i >= 0 in
filter (pos_or_zero, xs)
(* I. Take a list of int lists and return a list of int lists
by removing all the negative ints.
(int list list -> int list list) *)
let remove_negatives2 xss =
map (remove_negatives1, xss)
\ No newline at end of file
(* CSE 341, Section 03 *)
(* three parts:
1. Exceptions
2. Tail recursion
3. Using map and filter
*)
(* part 1: Exceptions (moved from Lecture 7) *)
exception MyUndesirableCondition
exception MyOtherException of int * int
let oh_no () =
raise MyUndesirableCondition
let oh_no_with_info () =
raise (MyOtherException (7, 42))
let catch_example () =
try oh_no ()
with MyUndesirableCondition -> 0
let catch_example_with_info () =
try oh_no_with_info ()
with MyOtherException (x, y) -> x + y
let boo () =
try oh_no ()
with MyOtherException (x, y) -> x + y
let hd xs =
match xs with
| [] -> raise MyUndesirableCondition
| x::_ -> x
let foo1 = hd [3;4;5]
(*let foo2 = hd []*)
let bar1 = try Some (hd [3;4;5]) with MyUndesirableCondition -> None
let bar2 = try Some (hd []) with MyUndesirableCondition -> None
let rec maxlist (xs,ex) = (* int list * exn -> int *)
match xs with
| [] -> raise ex
| x::[] -> x
| x::xs' -> let m = maxlist(xs',ex) in if x > m then x else m
let m1 = maxlist ([3;4;5],MyUndesirableCondition)
let m2 = try maxlist ([3;4;5],MyUndesirableCondition) with
MyUndesirableCondition -> 42
(*let m3 = maxlist ([],MyUndesirableCondition)*)
let m4 = try maxlist ([],MyUndesirableCondition) with
MyUndesirableCondition -> 42
(* part 2: tail-recursion practice *)
(* Example of a function already tail-recursive. *)
let rec second_to_last xs =
match xs with
| [] -> failwith "second_to_last: empty list"
| x::[] -> failwith "second_to_last: single-element list"
| x::_::[] -> x
| _::xs' -> second_to_last xs'
(* Example of making a function tail-recursive *)
let rec sum1 xs =
match xs with
| [] -> 0
| i::xs' -> i + sum1 xs'
let sum2 xs =
let rec f (xs,acc) =
match xs with
| [] -> acc
| i::xs' -> f (xs',i+acc) in
f (xs,0)
(* For you: count_empty_strings1 counts the number of empty strings in a list of strings *)
let rec count_empty_strings1 ss =
match ss with
| [] -> 0
| s::ss' -> (if s = "" then 1 else 0) + count_empty_strings1 ss'
let count_empty_strings2 ss =
let rec loop (acc,ss) =
match ss with
| [] -> acc
| s::ss' -> loop (acc+(if s = "" then 1 else 0), ss')
in loop (0,ss)
(* tail recursion and list order *)
(* try to make this tail recursive -- how can you fix the order? *)
let rec every_other1 xs =
match xs with
| [] -> xs
| x::[] -> xs
| x::_::xs' -> x :: every_other1 xs'
let every_other2 xs =
let rec loop (acc,xs) =
match xs with
| [] -> acc
| x::[] -> x::acc
| x::_::xs' -> loop (x::acc, xs')
in List.rev (loop ([],xs))
(* part 3: practice using map and filter *)
(* two super-useful higher-order functions on lists
get very comfortable with what they compute and their types
*)
let rec map (f, xs) =
match xs with
| [] -> []
| x :: xs' -> (f x) :: (map (f, xs'))
let rec filter (f, xs) =
match xs with
| [] -> []
| x :: xs' -> if f x then
x :: filter (f,xs')
else
filter (f,xs')
(* using map and filter *)
(* try to implement each of these with 2 lines by calling map or filter
(we will learn anonymous functions tomorrow, making them each 1 line instead of 2)
*)
(* A. Take a list of ints and return a new list with each int
divided by 2 -- example done for you
*)
let half_of_all xs =
let half_of i = i / 2 in
map (half_of, xs)
(* B. Take a list of floats and return a new float list with the
square root of the absolute value of each float.
(Hint: use Float.abs and sqrt) *)
let sqrts_of_abs_all fs = []
(* C. Take a list of strings and return a new string list with
each string capitalized. (Hint: use String.capitalize_ascii) *)
let capitalize ss = []
(* D. Take a list of pairs and return a list that contains
the first element of every pairs. *)
let firsts xs = []
(* E. Take a list of strings and return a string list that contains
only non-empty strings from the original list. *)
let no_empty_strings ss = []
(* F. Take a list of ints and return an int list that contains
only even numbers from the original list. *)
let only_evens xs = []
(* G. Take a list of ints and return an int list that contains
only ints that are power of 2.
You can use the provided funtion is_power_of_two. *)
let rec is_power_of_two i =
i > 0 &&
(i=1 || (i mod 2 = 0 && is_power_of_two (i / 2)))
let only_powers_of_two xs = []
(* H. Take a list of ints and return an int list by
removing all the negative ints. *)
let remove_negatives1 xs = []
(* I. Take a list of int lists and return a list of int lists
by removing all the negative ints.
(int list list -> int list list) *)
let remove_negatives2 xss = []
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