
Max
Starting from either exponential version of max
:
;; max : nonemptylistofnumbers number
(define (max alon)
(cond
[(null? (cdr alon)) (first alon)]
[else (cond
[(< (car alon) (max (cdr alon)))
(max (cdr alon))]
[else
(car alon)])]))
;; max : listofpositivenumbers number
(define (max alon)
(cond
[(null? alon) 0]
[else (cond
[(< (car alon) (max (car alon)))
(max (cdr alon))]
[else
(car alon)])]))
use let
to formulate an alternate version of that
only uses a linear number of recursive calls.
Solution
;; max : nonempty list of numbers number
(define (max alon)
(cond
[(null? (cdr alon)) (car alon)]
[else (let ([rst (max (cdr alon))])
(cond
[(< (car alon) rst)
rst]
[else
(car alon)]))]))
;; max : list of positive numbers number
(define (max alon)
(cond
[(null? alon) 0]
[else (let ([rst (max (cdr alon))])
(cond
[(< (car alon) rst)
rst]
[else
(car alon)]))]))

Best
Abstract over the comparison operator in max
and
produce a function best
that accepts a function
and returns the best element, according to that function.
Here's the header and purpose statement for best
:
;; best : (integer integer boolean) listofnumbers number
(define (best metric alon)
...)
Solution
(define (best metric alon)
(cond
[(null? alon) 0]
[else (let ([rst (best metric (cdr alon))])
(if (metric (car alon) rst)
(car alon)
rst))]))
Write both max
and min
in terms of
best
.
Solution
(define (max l) (best >= l))
(define (min l) (best <= l))

Filter
Write a function called filterlt7
that returns
all of the elements in a list that are less than
7
.
Solution
;; filterlt7 : listofnumbers listofnumbers
(define (filterlt7 alon)
(cond
[(null? alon) '()]
[else (if (<= (car alon) 7)
(cons (car alon) (filterlt7 (cdr alon)))
(filterlt7 (cdr alon)))]))
Write the function filter
that accepts a predicate
on numbers and returns all of the elements in the list that
satisfy the predicate. Here is the header and purpose
statement for the function:
;; filter : (number boolean) listofnumbers listofnumbers
(define (filter p alon)
...)
Solution
;; filter : (number boolean) listofnumbers listofnumbers
(define (filter p alon)
(cond
[(null? alon) '()]
[else (if (p (car alon))
(cons (car alon) (filter p (cdr alon)))
(filter p (cdr alon)))]))
Using filter
, redefine filterlt7
. Also,
define filterodd
, a function that accepts a list
and returns a list containing the odd numbers in its input.
Solution
(define (filterlt7 l)
(filter (lambda (x) (<= x 7)) l))
(define (filterodd l)
(filter odd? l))

Building Lists
Define a function called buildconsecutivelist
that accepts a number and returns a list of the number
from that number down to 1.
Solution
;; buildconsecutivelist : number (listof numbers)
(define (buildconsecutivelist n)
(cond
[(zero? n) '()]
[else
(cons n (buildconsecutivelist ( n 1)))]))
Define a function called buildlist
that accepts a
number n
and a function f
from numbers to
numbers and returns a list consisting of the results of
calling f
on the numbers from n
down to
1
.
Solution
;; buildlist : number (listof numbers)
(define (buildlist f n)
(cond
[(zero? n) '()]
[else
(cons (f n) (buildlist f ( n 1)))]))
Use buildlist
to redefine
buildconsecutivelist
and to define
buildperectsquares
, a function that returns
the first n
perfect squares.
Solution
(define (buildconsecutivelist l)
(buildlist (lambda (x) x) l))
(define (buildperfectsquares l)
(buildlist (lambda (x) (* x x)) l))

Binding Structure
Draw arrows to indicate the binding structure of these
programs [note: DrScheme's Check Syntax will tell you the
answers to this question, but you will not have Check
Syntax available to you on the exam. Be sure you can
figure these out by yourself]:
(define (f x)
x)
(f 1)
(define (len l)
(cond
[(null? l) 0]
[else (+ (len (cdr l)) 1)]))
(define (quad x)
(let ([sq (* x x)])
(* sq sq)))
(define (g x)
(let ([x x])
(+ x x)))
(define (g x)
(let ([x x]
[y x])
(+ x x)))
Solution
Use Check Syntax in DrScheme.