Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft...

40
Functional Programming III Spring 2014 Carola Wenk

Transcript of Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft...

Page 1: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Functional Programming III

Spring 2014Carola Wenk

Page 2: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge SortSuppose that we know how to merge two sorted lists. Then, we can sort recursively:

Merge Sort:

• 1. Split the given list into two equal parts.

• 2. Recursively sort each half.

• 3. Merge the sorted halves and return the result.

Page 3: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort (Python)

def merge_sort (L):n = len(L)#base case:if n<=1:

return L#recursive case: Recursively sort each halfA = merge_sort(L[:n/2]) # left half, L[0..n/2-1]B = merge_sort(L[n/2:]) # right half, L[n/2..n-1]# merge sorted halves:return merge(A,B)

Merge Sort:1. Split the given list into two equal parts.2. Recursively sort each half.3. Merge the sorted halves and return the result.

Page 4: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

5, 25, 55, 1 8, 10, 11, 2

5, 25 55, 1 8, 10 11, 2

5 25 55 1 8 10 11 2

Actually, not a lot is happening in the recursive calls. So where is the sorting happening?

Recursive Calls

Page 5: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

5, 25, 55, 1 8, 10, 11, 2

5, 25 55, 1 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 6: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

5, 25, 55, 1 8, 10, 11, 2

5, 25 55, 1 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 7: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

5, 25, 55, 1 8, 10, 11, 2

5, 25 55, 1 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 8: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

5, 25, 55, 1 8, 10, 11, 2

5, 25 55, 1 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 9: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

5, 25, 55, 1 8, 10, 11, 2

5, 25 1, 55 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 10: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

1, 5, 25, 55 8, 10, 11, 2

5, 25 1, 55 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 11: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

1, 5, 25, 55 8, 10, 11, 2

5, 25 1, 55 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 12: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

1, 5, 25, 55 8, 10, 11, 2

5, 25 1, 55 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 13: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

1, 5, 25, 55 8, 10, 11, 2

5, 25 1, 55 8, 10 11, 2

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 14: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

1, 5, 25, 55 8, 10, 11, 2

5, 25 1, 55 8, 10 2, 11

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 15: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

5, 25, 55, 1, 8, 10, 11, 2

1, 5, 25, 55 2, 8, 10, 11

5, 25 1, 55 8, 10 2, 11

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 16: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort

1, 2, 5, 8, 10, 11, 25, 55

1, 5, 25, 55 2, 8, 10, 11

5, 25 1, 55 8, 10 2, 11

5 25 55 1 8 10 11 2

The merge step is actually doing all of the work!

Page 17: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10Sorted List A Sorted List B

?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?

Page 18: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 19: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 20: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, ?, ?, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 21: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, ?, ?, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 22: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, ?, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 23: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, ?, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 24: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 25: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, ?, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 26: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 27: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, ?, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 28: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 29: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

• Suppose that we instead had a list that had two sorted halves. Could we do better?

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, ?, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 30: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

• Suppose that we instead had a list that had two sorted halves. Could we do better?

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 31: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, ?, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 32: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 33: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, ?, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 34: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, 9, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 35: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, 9, ?, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 36: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 37: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ?

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 38: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Sorted List A Sorted List B

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Merging Lists

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

Page 39: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merging Lists

Sorted List A Sorted List B

The key idea is to scan through both lists, while moving the smallest element to a new list. If we finish scanning either list, the rest of the other list is appended to the result.

1, 4, 6, 8, 11 2, 3, 5, 7, 9, 10

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Page 40: Functional Programming III - Tulane Universitycarola/teaching/cmps1600/spring14/...Microsoft PowerPoint - FunctionalProgramming_3.pptx Author carola Created Date 4/10/2014 10:52:39

Merge Sort• Functional programming languages are ideally suited

to implement recursive algorithms. How would we implement merge sort?

(define (merge-sort L)(if (equal? (length L) 1)

L(let ([mid (quotient (length L) 2)])

(merge (merge-sort (take L mid))(merge-sort (drop L mid))))))

Assuming merge is correct, is merge-sort correct?

How do we implement merge?