Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design.

Slides:



Advertisements
Similar presentations
Type Variables in ML Until we know the type of a value (perhaps never!), we use a variable for its type Book uses, e.g., t1, tx, tf PL literature uses.
Advertisements

CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Jyotishka Datta STAT 598Z – Sorting. Insertion Sort If the first few objects are already sorted, an unsorted object can be inserted in the sorted set.
Tutorial 5. Recap For a function f which takes arguments arg1, arg2,... argn, and returns type rettype, the compiler will print: f : arg1 -> arg2 ->...
Functional Design and Programming Lecture 11: Functional reasoning.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Quick-Sort     29  9.
Recitation on analysis of algorithms. runtimeof MergeSort /** Sort b[h..k]. */ public static void mS(Comparable[] b, int h, int k) { if (h >= k) return;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
Cse536 Functional Programming Lecture #14, Nov. 10, 2004 Programming with Streams –Infinite lists v.s. Streams –Normal order evaluation –Recursive streams.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Functional Design and Programming Lecture 4: Sorting.
Data Structures Review Session 1
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting - 3 CS 202 – Fundamental Structures of Computer Science II.
Induction and recursion
Lecture 2 MAS 714 Hartmut Klauck
Problem 1.b Theorem 1: For all n≥0, the function call fac n evaluates to an answer n!: let rec fac n = if n
Fall 2013 Instructor: Reza Entezari-Maleki Sharif University of Technology 1 Fundamentals of Programming Session 17 These.
1 Lecture 16: Lists and vectors Binary search, Sorting.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Sorting Fun1 Chapter 4: Sorting     29  9.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Sorting 2 Taking an arbitrary permutation of n items and rearranging them into total order Sorting is, without doubt, the most fundamental algorithmic.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CS 46B: Introduction to Data Structures July 2 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Chapter SevenModern Programming Languages1 A Second Look At ML.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Functional Programming Lecture 3 - Lists Muffy Calder.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Induction and Recursion CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Chapter 23 Sorting CS1: Java Programming Colorado State University
Sorting Chapter 14.
Recursive Algorithms Section 5.4.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Divide and Conquer.
Advanced Sorting Methods: Shellsort
Data Structures Review Session
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
CSE 332: Sorting II Spring 2016.
Advanced Sorting Methods: Shellsort
Presentation transcript:

Kyung-Goo Doh Hanyang University - ERICAComputer Science & Engineering Functional Programming / Imperative Programming CSE215 Fundamentals of Program Design Fall List Processing Version 2.0

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] # ;; (+ append +) - : int list = [1; 2; 3; 4; 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 Standard Library: List

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.

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

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)   5 Complexity - time: θ(n) - space: θ(n) let rec length xs = match xs with | [] -> 0 | _::xs -> 1 + length xs

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

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

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

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: is the infix version of this append function

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, for all lists xs and ys. Proof: We prove by structural induction on xs. (Basis) xs = [], append [] ys = ys by definition of program ‘append’ = by property of concatenation (Induction hypothesis) For some list xs of length ≥ 0, append xs ys evaluates to (Induction step) It suffices to show that append (x::xs) ys evaluates to append (x::xs) ys = x :: append xs ys by definition of program ‘append’ = x :: by induction hypothesis = 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 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 [x] ;; val rev : 'a list -> 'a list = Primitive Complexity - time: θ(n 2 ) - space: θ(n) rev [1;2;3;4]  rev [1]  rev [1]  rev [1]  [1]  [1]  [1]  [1]  [1]  [4;3;2;1] Execution Trace

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 [x] by definition of program ‘rev’ = [x] by induction hypothesis = ~(x::xs) by inspection let rec rev xs = match xs with | [] -> [] | x::xs -> rev [x]

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

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 by structural induction on xs. (Basis) xs = [], loop [] ys = ys by definition of program ‘loop’ = by property = by inspection (Induction hypothesis) For some list xs of length ≥ 0, loop xs ys evaluates to (Induction step) It suffices to show that loop (x::xs) ys evaluates to loop (x::xs) ys = loop xs (x::ys) by definition of program ‘loop’ = by induction hypothesis = = by inspection Thus rev xs = loop xs [] evaluates to = ~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 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)

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, 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 [1]. append xs ys = loop (rev xs) ys by definition of ‘append’ = loop ~xs ys by previous theorem = by [1] = 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 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.

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.

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

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

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.

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

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

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.

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

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'

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)

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.

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

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 (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)