Next: Higher-Order Functions (10 points)
Up: CS212 Preliminary Exam I Previous:
Evaluation of Dylan Expressions
You are to analyze the running time for a function tail-sums (defined below). The
function takes a list of numbers, say n1, n2,
... , nm and returns a list of numbers s1,
s2, ... , sm where .
(define (sum-list <function>)
(method ((x <list>))
(if (null? x) 0 (+
(head x) (sum-list (tail x))))))
(define (tail-sums <function>)
(method ((y <list>))
(if (null? x) '() (pair
(+ (head x) (sum-list (tail x)))
(tail-sums
(tail x))))))
For example: (tail-sums '(1 2 3 4 5)) evaluates to the list '(15 14 12 9 5).
(define (tail-sums-2 <function>) (method ((x <list>)) (cond ((null? x) '()) ((null? (tail x)) x) (else: (bind (((t <list>) (tail-sums-2 (tail x))) ((i <integer>) (+ (head x) (head t)))) (pair i t))))))
Next: Higher-Order Functions (10 points)
Up: CS212 Preliminary Exam I Previous:
Evaluation of Dylan Expressions