1 / 9

Creating Functional Programs

Creating Functional Programs. Brad Vander Zanden. Basic Techniques. Tail Recursion Use continuation arguments if necessary Akin to pre-processing a list Inductive Construction Akin to post-processing a list. Factorial. Inductive Construction (define fact (lambda (n) ( cond

Download Presentation

Creating Functional Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating Functional Programs Brad Vander Zanden

  2. Basic Techniques • Tail Recursion • Use continuation arguments if necessary • Akin to pre-processing a list • Inductive Construction • Akin to post-processing a list

  3. Factorial • Inductive Construction (define fact (lambda (n) (cond ((= n 0) 1) ((= n 1) 1) (#t (* n (fact (- n 1)))))))

  4. Factorial • Tail Recursion Construction (define fact (lambda (n) (letrec((factHelper (lambda (n productThusFar) (cond ((= n 0) productThusFar) ((= n 1) productThusFar) (#t (factHelper (- n 1) (* n productThusFar))))))) (factHelper n 1))))

  5. Sum of Numbers • Inductive Construction (define sumList (lambda (L) (cond ((null? L) 0) (#t (+ (car L) (sumList (cdr L)))))))

  6. Sum of Numbers • Tail Recursion Construction (define sumList (lambda (L) (letrec((sumListHelper (lambda (L sumThusFar) (cond ((null? L) sumThusFar) (#t (sumListHelper (cdr L) (+ (car L) sumThusFar))))))) (sumListHelper L 0))))

  7. Quicksort • Inductive Construction (define qsortPartition (lambda (pivot L) (if (null? L) (cons '() '()) (let* ((result (qsortPartition pivot (cdr L))) (lesserList (car result)) (greaterList (cdr result))) (if (< (car L) pivot) ; add the head of L to the lesserList (cons (cons (car L) lesserList) greaterList) ; add the head of L to the greaterList (cons lesserList (cons (car L) greaterList)))))))

  8. Quicksort • Tail Recursion Construction (define qsortPartition (lambda (pivot L L1 L2) (if (null? L) (cons L1 L2) ; return L1 and L2 once L is exhausted (let ((firstElement (car L))) (if (< firstElement pivot) ; add head of L to L1 and partition rest of L (qsortPartition pivot (cdr L) (cons firstElement L1) L2) ; add head of L to L2 and partition rest of L (qsortPartition pivot (cdr L) L1 (cons firstElement L2)))))))

  9. Quicksort • Either construction (define qsort (lambda (L) (if (null? L) L (let* ((result (qsortPartition (car L) (cdr L) '() '())) (lesserList (car result)) (greaterList (cdr result))) (append (qsortlesserList) (list (car L)) (qsortgreaterList))))))

More Related