Functional Problem Solving (CSC 151 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
My quiz policies will continue through this week. You may bring a page of notes. You will not have a time limit, although I will encourage you to try to finish in ten minutes.
cons, car, cdr, and null?
one-two, won-too, and want-to)image-compute!
let and let*
let and lambdaContext:
; Given a list, find out how many values are in the list
(define my-length
(lambda (lst)
(if (null? lst)
0
(+ 1 (my-length (cdr lst))))))
How does this work?
(my-length '(1 2 3 4))
=> (if (null? '(1 2 3 4))
0
(+ 1 (my-length (cdr '(1 2 3 4)))))
=> (if #f
0
(+ 1 (my-length (cdr '(1 2 3 4)))))
=> (+ 1 (my-length (cdr '(1 2 3 4))))
=> (+ 1 (my-length '(2 3 4)))
=> (+ 1 (if (null? '(2 3 4))
0
(+ 1 (my-length (cdr '(2 3 4))))))
=> (+ 1 (if #f
0
(+ 1 (my-length (cdr '(2 3 4))))))
=> (+ 1 (+ 1 (my-length (cdr '(2 3 4)))))
=> (+ 1 (+ 1 (my-length '(3 4))))
=> (+ 1 (+ 1 (if (null? '(3 4))
0
(+ 1 (my-length (cdr '(3 4)))))))
...
=> (+ 1 (+ 1 (+ 1 (my-length (cdr '(3 4))))))
=> (+ 1 (+ 1 (+ 1 (my-length '(4)))))
=> (+ 1 (+ 1 (+ 1 (if (null? '(4))
0
(+ 1 (my-length (cdr '(4))))))))
...
=> (+ 1 (+ 1 (+ 1 (+ 1 (my-length (cdr '(4)))))))
=> (+ 1 (+ 1 (+ 1 (+ 1 (my-length null)))))
=> (+ 1 (+ 1 (+ 1 (+ 1 (if (null? null)
0
(+ 1 (my-length (cdr null))))))
=> (+ 1 (+ 1 (+ 1 (+ 1 (if #t
0
(+ 1 (my-length (cdr null))))))
=> (+ 1 (+ 1 (+ 1 (+ 1 0))))
=> (+ 1 (+ 1 (+ 1 1)))
=> (+ 1 (+ 1 2))
=> (+ 1 3)
=> 4
; Given a list of integers, count even numbers in the list
(define count-evens
(lambda (lst)
(cond
[(null? lst) 0]
[(even? (car lst)) (+ 1 (count-evens (cdr-list)))]
[else (count-evens (cdr-list))])))
; Given a list of integers, extract the even values and
; give back a list of those values
(define list-evens
(lambda (lst)
(cond
[(null? lst) null]
[(even? (car lst)) (cons (car lst) (list-evens (cdr-list)))]
[else (list-evens (cdr-list))])))
(define PROC
(lambda (lst)
(if (null? lst)
SOME-VALUE
(COMBINE (car lst) (PROC (cdr lst))))))
(define PROC
(lambda (lst)
(cond
[(null lst?) SOME-VALUE]
[(TEST (car lst?))
(COMBINE (car lst) (PROC (cdr lst)))]
[else
...])))