- Using the following implementation of positive integers:
(define zero '())
(define (zero? n) (null? n))
(define (add1 n) (cons 'a n))
(define (sub1 n) (cdr n))
Restricting yourself to the interface (zero, zero?,
add1, sub1), provide definitions for the following:
- The value one
- The binary addition procedure, i.e.,
(define (add a b)
;; insert body here
)
- The binary subtraction procedure
-
What is the difference between the following two Scheme definitions?
(define (increment n) (+ n 1))
(define increment (lambda (n) (+ n 1)))
- The next few questions will use the following procedure:
(define (accumulate op init r)
(if (null? r)
init
(op (first r) (accumulate op init (rest r)))))
- What is value of (accumulate + 0 '(1 2 3 4)) ?
- What is value of (accumulate - 0 '(1 2 3 4)) ?
- What is value of (accumulate * 0 '(1 2 3 4)) ?
- Using accumulate and the following binary operator,
write a procedure that takes a list of any number of elements and
multiplies them all together
(define (product a b)
(if (= a 1)
b
(+ b (product (- a 1) b))))
- Write a definition for list-append (as seen last homework)
in terms of accumulate.
- Write a definition for list-length (as seen last homework)
in terms of accumulate.