Download presentation
Presentation is loading. Please wait.
Published byZoe Casey Modified over 9 years ago
1
Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Fall 2009 06 List Processing Version 2.0
2
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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]
3
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Standard Library: List
4
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Definition: Lists in OCaml Structural Inductive Definition of Lists of type ‘a 1. (basis) An empty list, [], is a list of type ‘a. 2. (induction) If xs is a list of values of type ‘a and x is a value of type ‘a, then so is x::xs. 3. Nothing else is a list. Structural Induction 1. 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.
5
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Computing List Length # let rec length xs = match xs with | [] -> 0 | _::xs -> 1 + length xs ;; val length : 'a list -> int = # 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 = Primitive Tail
6
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
7
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
8
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
9
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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 inspection Thus 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
10
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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 = Primitive 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
11
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
12
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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 = Primitive Complexity - time: θ(n 2 ) - 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
13
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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]
14
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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 = 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
15
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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 inspection Thus 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 []
16
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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 = 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)
17
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
18
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Sorting sort : ‘a list -> ‘a list input: the list of values of type output: the rearranged list of values in ascending order.
19
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Selection Sort Algorithm 1. 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.
20
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Animation Selection Sort
21
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
22
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Insertion Sort Algorithm 1. 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.
23
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Animation Insertion Sort
24
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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
25
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Merge Sort Algorithm 1. 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.
26
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Animation Merge Sort
27
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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'
28
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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)
29
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Quicksort Algorithm 1. 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.
30
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-Goo Doh Animation Quicksort
31
Hanyang University - ERICA Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Computer Science & Engineering CSE215 Fundamentals of Program Design 06 List Processing Fall 2009 Kyung-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)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.