Key to CSE341/413 Final, Spring 2026 handout #13 1. Scheme expressions. Part A: (define a 2) (define b 3) (define c 5) (let ((b (* a c)) (a (* b c))) (list a b c)) '(15 10 5) Part B: (define a 2) (define b 3) (define c 5) (let* ((b (* a b)) (a (* a b))) (list a b c)) '(12 6 5) Part C: (define x 2) (define (f n) (* x y n)) (define y 3) (define a (f 7)) (set! x 5) (define b (f 2)) (define c a) (set! a 13) (list a b c) '(13 30 42) Part D: (define a '(1 2 3)) (define b (cons 1 (cdr a))) (define c (append '(0) b)) (list c (eq? a b) (eq? (cdr a) (cdr b)) (eq? (cdr c) b)) '((0 1 2 3) #f #t #t) 2. Two possible solutions appear below. (define (sum-positives lst) (cond [(null? lst) 0] [(> (car lst) 0) (+ (car lst) (sum-positives (cdr lst)))] [else (sum-positives (cdr lst))])) (define (sum-positives lst) (foldl + 0 (filter (lambda (n) (> n 0)) lst))) 3. One possible solution appears below. (define (prefix-apply f pre lst) (if (null? lst) '() (cons pre (cons (f (car lst)) (prefix-apply f pre (cdr lst)))))) 4. One possible solution appears below. (define (average lst) (define (helper sum rest) (if (null? rest) (/ sum (length lst)) (helper (+ sum (car rest)) (cdr rest)))) (if (null? lst) (error "empty list") (helper 0 lst))) 5. Two possible solutions appear below. (define (join lst separator) (define (helper result rest) (if (null? rest) result (helper (string-append result separator (number->string (car rest))) (cdr rest)))) (if (pair? lst) (helper (number->string (car lst)) (cdr lst)) "")) (define (join lst separator) (if (pair? lst) (apply string-append (cdr (prefix-apply number->string separator lst))) "")) 6. One possible solution appears below. (define (sublist lst start stop) (define (helper index current) (cond [(= index stop) '()] [(null? current) (error "illegal sublist")] [(>= index start) (cons (car current) ( helper (+ index 1) (cdr current)))] [else (helper (+ index 1) (cdr current))])) (if (or (> start stop) (< start 0) (< stop 0)) (error "illegal sublist") (helper 0 lst))) 7. One possible solution appears below. (define (convert data) (cond [(not (list? data)) data] [(< (length data) 2) (error "illegal expression")] [else (let ([operator (car data)] [first (convert2 (cadr data))]) (cons first (prefix-apply convert2 operator (cddr data))))])) 8. Ruby expressions. Part A: y.each {|s| puts s + " " + s} foo foo bar bar baz baz mumble mumble Part B: y.find_all {|s| s.length == 3} ["foo", "bar", "baz"] Part C: z = [] for n in x do z.push -n end [2, 1, 0, -1, -2] 91. Two possible solutions appear below. class Integer def first return self.abs.to_s[0].to_i end end class Integer def first result = self.abs result /= 10 while result >= 10 return result end end 10. One possible solution appears below. class Array def partition part1 = [] part2 = [] for n in self do if yield(n) then part1 << n else part2 << n end end return [part1, part2] end end 11. One possible solution appears below. def anagrams(word, dictionary) result = [] for other in dictionary do copy1 = word.downcase.split("").sort copy2 = other.downcase.split("").sort result << other if (copy1 == copy2) end return result end 12. One possible solution appears below. class Array def analyze count = Hash.new(0) self.each {|n| count[n.first] += 1} for n in 1..9 do pct = 100.0 * count[n] / self.length print n, " = ", count[n], " (", pct.round, "%)\n" end end end
Stuart Reges
Last modified: Thu Jun 11 13:20:03 PDT 2026