Upload
syshe
View
22
Download
1
Embed Size (px)
DESCRIPTION
06 List Processing. Version 2.0. Predefined Polymorphic Lists. # [] ;; (+ Nil +) - : 'a list = [] # 1::[] ;; (+ Cons +) - : int list = [1] # true::[];; - : bool list = [true] # 1::2::3::4::5::[] ;; - PowerPoint PPT Presentation
Citation preview
Kyung-Goo Doh
Hanyang University - ERICA Computer Science & Engineering
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
CSE215 Fundamentals of Program Design Fall2009
06
List Processing
Version 2.0
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Predefined Polymorphic Lists# [] ;; (+ Nil +)- : 'a list = []# 1::[] ;; (+ Cons +)- : int list = [1]# true::[];;- : bool list = [true]# 1::2::3::4::5::[] ;;- : int list = [1; 2; 3; 4; 5]# [1;2;3;4;5] ;;- : int list = [1; 2; 3; 4; 5]# 1::2::[3;4;5] ;;- : int list = [1; 2; 3; 4; 5]# List.hd [1;2;3;4;5] ;; (+ head +)- : int = 1# List.tl [1;2;3;4;5] ;; (+ tail +)- : int list = [2; 3; 4; 5]# [1;2;3]@[4;5] ;; (+ append +)- : int list = [1; 2; 3; 4; 5]
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Standard Library: List
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Definition: Lists in OCaml
Structural Inductive Definition of Lists of type ‘a (basis) An empty list, [], is a list of type ‘a.
(induction) If xs is a list of values of type ‘a and x is a value of type ‘a, then so is x::xs.
Nothing else is a list.
Structural Induction1. is P([]) true?
2. Assume P(xs) is true, is P(x::xs) true?
3. If we can say “yes” to these two questions, then we can assure that P(xs) is true for all lists xs.
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computing List Length# let rec length xs = match xs with | [] -> 0 | _::xs -> 1 + length xs ;;val length : 'a list -> int = <fun># length [3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3;3] ;;- : int = 18# length [] ;;- : int = 0
# let length xs = let rec loop xs n = match xs with | [] -> n | _::xs -> loop xs (n+1) in loop xs 0 ;;val length : 'a list -> int = <fun>
Prim
itive
Tail
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computation Trace (Primitive list length)
length [1;2;3;4;5] 1 + length [2;3;4;5] 1 + (1 + length [3;4;5]) 1 + (1 + (1 + length [4;5])) 1 + (1 + (1 + (1 + length [5]))) 1 + (1 + (1 + (1 + (1 + length [])))) 1 + (1 + (1 + (1 + (1 + 0)))) 1 + (1 + (1 + (1 + 1))) 1 + (1 + (1 + 2)) 1 + (1 + 3) 1 + 4 5
Complexity- time: θ(n)- space: θ(n)
let rec length xs = match xs with | [] -> 0 | _::xs -> 1 + length xs
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computation Trace (Tail list length)
length [1;2;3;4;5] loop [1;2;3;4;5] 0 loop [2;3;4;5] 1 loop [3;4;5] 2 loop [4;5] 3 loop [5] 4 loop [] 5 5
Complexity- time: θ(n)- space: θ(1)
let length xs = let rec loop xs n = match xs with | [] -> n | _::xs -> loop xs (n+1) in loop xs 0
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Verification (Primitive list length)
Theorem : The primitive recursive function call length xs evaluates to the length of xs, |xs|, for all lists xs.
Proof: We prove by structural induction on xs. (Basis) xs = [], length [] = 0 by definition of program ‘length’ = |[]| by inspection (Induction hypothesis) For some list xs of length ≥ 0, length xs evaluates to |xs| (Induction step) It suffices to show that length (x::xs) evaluates to |x::xs|
length (x::xs) = 1 + length xs by definition of program length
= 1 + |xs| by induction hypothesis = |x::xs| by inspection
let rec length xs = match xs with | [] -> 0 | _::xs -> 1 + length xs
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Verification (Tail list length)
Theorem : The tail recursive function call length xs evaluates to the length of xs, |xs|, for all lists xs.
Proof: We first show that loop xs n evaluates to n+|xs| by structural induction on xs. (Basis) xs = [], loop [] n = n by definition of program ‘loop’ = n + 0 by algebra = n + |[]| by inspection (Induction hypothesis) For some list xs of length ≥ 0, loop xs n evaluates to n+|
xs| (Induction step) It suffices to show that loop (x::xs) n evaluates to n+|x::xs|
loop (x::xs) n = loop xs (n+1) by definition of program loop
= n+1+|xs| by induction hypothesis = n+|x::xs| by inspectionThus length xs = loop xs 0 evaluates to 0+|xs| = |xs|.
let length xs = let rec loop xs n = match xs with | [] -> n | _::xs -> loop xs (n+1) in loop xs 0
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computing List Append (* Concatenate two lists next to each other *)# let rec append xs ys = match xs with | [] -> ys | x::xs -> x::append xs ys ;;val append : 'a list -> 'a list -> 'a list = <fun>
Prim
itive
append [1;2;3] [4;5] 1 :: append [2;3] [4;5] 1 :: 2 :: append [3] [4;5] 1 :: 2 :: 3 :: append [] [4;5] 1 :: 2 :: 3 :: [4;5] 1 :: 2 :: [3;4;5] 1 :: [2;3;4;5] [1;2;3;4;5]
Execution Trace
Complexity- time: θ(n)- space: θ(n)
@ is the infix version of this append function
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Verification (Primitive list append)
Theorem : The primitive recursive function call append xs ys evaluates to the concatenation of lists xs and ys, xs@ys, for all lists xs and ys.
Proof: We prove by structural induction on xs. (Basis) xs = [], append [] ys = ys by definition of program
‘append’ = []@ys by property of concatenation (Induction hypothesis) For some list xs of length ≥ 0, append xs ys evaluates to
xs@ys (Induction step) It suffices to show that append (x::xs) ys evaluates to (x::xs)@ys
append (x::xs) ys = x :: append xs ys by definition of program ‘append’
= x :: (xs@ys) by induction hypothesis = (x::xs)@ys by property of :: and
concatenation
let rec append xs ys = match xs with | [] -> ys | x::xs -> x::append xs ys
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computing List Reverse
(* Rearrange a list in reverse order*)# let rec rev xs = match xs with | [] -> [] | x::xs -> rev xs @ [x] ;;val rev : 'a list -> 'a list = <fun>
Prim
itive
Complexity- time: θ(n2)- space: θ(n)
rev [1;2;3;4] rev [2;3;4] @ [1] rev [3;4] @ [2] @ [1] rev [4] @ [3] @ [2] @ [1] rev[] @ [4] @ [3] @ [2] @ [1] [] @ [4] @ [3] @ [2] @ [1] [4] @ [3] @ [2] @ [1] [4;3] @ [2] @ [1] [4;3;2] @ [1] [4;3;2;1]
Execution Trace
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Verification (Primitive list reverse)
Theorem : The primitive recursive function call reverse xs evaluates to the reverse of xs, ~xs, for all lists xs.
Proof: We prove by structural induction on xs. (Basis) xs = [], rev [] = [] by definition of program ‘rev’ = ~[] by inspection (Induction hypothesis) For some list xs of length ≥ 0, rev xs evaluates to ~xs (Induction step) It suffices to show that rev (x::xs) evaluates to ~(x::xs)
rev (x::xs) = rev xs @ [x] by definition of program ‘rev’ = ~xs @ [x] by induction hypothesis = ~(x::xs) by inspection
let rec rev xs = match xs with | [] -> [] | x::xs -> rev xs @ [x]
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computing List Reverse
(* Rearrange a list in reverse order*)# let rev xs = let rec loop xs ys = match xs with | [] -> ys | x::xs -> loop xs (x::ys) in loop xs [] ;;val rev : 'a list -> 'a list = <fun>
Tail
Complexity- time: θ(n)- space: θ(1)
rev [1;2;3;4] loop [1;2;3;4] [] loop [2;3;4] [1] loop [3;4] [2;1] loop [4] [3;2;1] loop [] [4;3;2;1] [4;3;2;1]
Execution Trace
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Verification (Tail list reverse)
Theorem : The tail recursive function call rev xs evaluates to the reverse of xs, ~xs, for all lists xs.
Proof: We first show that loop xs ys evaluates to ~xs@ys by structural induction on xs. (Basis) xs = [], loop [] ys = ys by definition of program ‘loop’ = []@ys by property of @ = ~[]@ys by inspection (Induction hypothesis) For some list xs of length ≥ 0, loop xs ys evaluates to
~xs@ys (Induction step) It suffices to show that loop (x::xs) ys evaluates to ~(x::xs)@ys
loop (x::xs) ys = loop xs (x::ys) by definition of program ‘loop’
= ~xs@(x::ys) by induction hypothesis = ~xs@[x]@ys = ~(x::xs)@ys by inspectionThus rev xs = loop xs [] evaluates to ~xs@[] = ~xs
let rev xs = let rec loop xs ys = match xs with | [] -> ys | x::xs -> loop xs (x::ys) in loop xs []
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Computing List Append# let append xs ys = let rec loop xs ys = match xs with | [] -> ys | x::xs -> loop xs (x::ys) in loop (rev xs) ys ;;val append : 'a list -> 'a list -> 'a list = <fun>
Tail
append [1;2;3] [4;5] loop (rev [1;2;3]) [4;5] loop (loop [1;2;3] []) [4;5] loop (loop [2;3] [1]) [4;5] loop (loop [3] [2;1]) [4;5] loop (loop [] [3;2;1]) [4;5] loop [3;2;1] [4;5] loop [2;1] [3;4;5] loop [1] [2;3;4;5] loop [] [1;2;3;4;5] [1;2;3;4;5]
Execution Trace
Complexity- time: θ(n)- space: θ(1)
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Verification (Tail list append)
Theorem : The tail recursive function call append xs ys evaluates to the concatenation of xs and ys, xs@ys, for all lists xs and ys.
Proof: The ‘loop’ function here is identical to the ‘loop’ function in the tail recursive version of ‘rev’. Thus we already proved that loop xs ys evaluates to ~xs@ys [1] . append xs ys = loop (rev xs) ys by definition of ‘append’
= loop ~xs ys by previous theorem = ~(~xs)@ys by [1] = xs@ys by property of reverse
let append xs ys = let rec loop xs ys = match xs with | [] -> ys | x::xs -> loop xs (x::ys) in loop (rev xs) ys
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Sorting
sort : ‘a list -> ‘a list input: the list of values of type output: the rearranged list of values in ascending order.
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Selection Sort
Algorithm1. Initialization
unsorted list = input list
sorted list = empty
2. Find the minimum value in the unsorted list.
3. Move it to the back of the sorted list.
4. Repeat the steps 2~3 until the unsorted list is empty.
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Animation Selection Sort
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Program Selection Sort
(* find_min : ‘a list -> ‘a * ‘a list *)let rec find_min xs = match xs with | y::[] -> (y,[]) | y::ys -> let (m,zs) = find_min ys in if y<m then (y,ys) else (m,y::zs) | [] -> failwith "This message will never be displayed"
(* selection_sort : 'a list -> 'a list *)let rec selection_sort xs = match xs with | [] -> [] | _ -> let (m,ys) = find_min xs in m :: selection_sort ys
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Insertion Sort
Algorithm1. Initialization
unsorted list = input list
sorted list = empty
2. Remove a value from the unsorted list.
3. Insert it at the correct position in the sorted list.
4. Repeat the steps 2~3 until the unsorted list is empty.
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Animation Insertion Sort
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Program Insertion Sort
(* insert : 'a list -> 'a -> 'a list *)let rec insert xs x = match xs with | [] -> [x] | y::ys -> if y<x then y :: insert ys x else x :: xs
(* insertion_sort : 'a list -> 'a list *)let insertion_sort xs = let rec loop sorted unsorted = match unsorted with | [] -> sorted | y::ys -> loop (insert sorted y) ys in loop [] xs
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Merge Sort
Algorithm1. If the list is of length 0 or 1, then it is already sorted.
2. Otherwise:
Split the unsorted list into two sublists of about half the size.
Sort each sublist recursively by re-applying merge sort.
Merge the two sorted sublist back into one sorted list.
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Animation Merge Sort
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Program Merge Sort ~
(* split : 'a list -> 'a list * 'a list *)let rec split xs = match xs with | [] -> ([],[]) | [x] -> ([x],[]) | x::xs' -> let (ls,rs) = split xs' in if List.length ls <= List.length rs then (x::ls,rs) else (ls,x::rs)
(* merge : 'a list -> 'a list -> 'a list *)let rec merge xs ys = match (xs,ys) with | ([],_) -> ys | (_,[]) -> xs | (x::xs',y::ys') -> if x<y then x :: merge xs' ys else y :: merge xs ys'
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Program Merge Sort
(* merge_sort : 'a list -> 'a list *)let rec merge_sort xs = match xs with | [] -> [] | [x] -> [x] | _ -> let (ls,rs) = split xs in merge (merge_sort ls) (merge_sort rs)
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Quicksort
Algorithm1. Pick an element, called a pivot, from the list.
2. [Partition] Reorder the list so that all elements which are less than the pivot come
before the pivot and
so that all elements greater than the pivot come after it
3. Recursively sort the sublist of lesser elements and the sublist of greater elements.
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Animation Quicksort
Hanyang University - ERICA
Functional Programming / Imperative ProgrammingCSE215 Fundamentals of Program Design
Computer Science & Engineering
CSE215 Fundamentals of Program Design06 List Processing
Fall 2009Kyung-Goo Doh
Program Quicksort
(* quicksort : 'a list -> 'a list *)let rec quicksort xs = match xs with | [] -> [] | pivot::xs' -> let (ls,rs) = partition pivot xs' in (quicksort ls) @ [pivot] @ (quicksort rs)
(* partition : 'a -> 'a list -> 'a list * 'a list *)let partition pivot xs = (List.filter (fun x -> x < pivot) xs, List.filter (fun x -> pivot <= x) xs)