Functional Problem Solving (CSC 151 2016S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [Remote] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2016S)] [Davis (2013F)] [Rebelsky (2015F)] [Weinman (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
(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
Writeup 27: 3h, Verifying Preconditions Lab