Functional Problem Solving (CSC 151 2016S) : EBoards

CSC151.02 2016S, Class 27: Preconditions, Revisited


Overview

Preliminaries

Admin

Reminders

Upcoming Work:

Extra Credit

Academic / Artistic

Peer

Regular Peer

Misc

Far in the Future

No Extra Credit, But Still Good

Questions

Recursion Lab

(define list-length
  (lambda (lst)
    (if (null? lst)
        0
        (+ 1 (list-length (cdr lst))))))

(list-length '(1 2 3))
; Is it null? No
=> (+ 1 (list-length (cdr '(1 2 3))))
=> (+ 1 (list-length '(2 3)))
; list-length is innermost operation.  
; is '(2 3) null?  NO!  So we replace the inner list-length
; with the alternate
=> (+ 1 (+ 1 (list-length (cdr '(2 3)))))
=> (+ 1 (+ 1 (list-length '(3))))
; list-length is innermost operation.  
; is '(3) null?  NO!  So we replace the inner list-length
=> (+ 1 (+ 1 (+ 1 (list-length (cdr '(3))))))
=> (+ 1 (+ 1 (+ 1 (list-length '()))))
; list-length is innermost operation.  
; is '() null?  YES!  Replace with 0
=> (+ 1 (+ 1 (+ 1 0)))
=> (+ 1 (+ 1 1))
=> (+ 1 2)
=> 3

Can we think about the base case for product?

> (*)
1

Okay, why? Because 1 is the multiplicative identify. That means,

> (product '())
1

What's the writeup?

Writeup 26: 6ad, Recursion Basics Lab

Preconditions Lab

Writeup 27: 3h, Verifying Preconditions Lab