Taras Bunyk

SICP 2.2.1 Sequences

Published: 2020-07-16T21:39:38.000Z

Finally, the thing Lisp was named after.

Fix MIT Scheme to be like SICP scheme using this:

(define nil '())

Exercise 2.17

(define (last-pair l)
  (if (null? (cdr l))
    (last-pair (cdr l))
(last-pair (list 23 72 149 34))

Exercise 2.18

(define (reverse l)
  (define (iter src dst)
    (if (null? src)
      (iter (cdr src) (cons (car src) dst))
  (iter l nil)
(reverse (list 1 2 3 4))

Exercise 2.19

(define us-coins (list 50 25 10 5 1))
(define uk-coins (list 100 50 20 10 5 2 1 0.5))

(define (cc amount coin-values)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (no-more? coin-values)) 0)
         (+ (cc amount
                (except-first-denomination coin-values))
            (cc (- amount
                   (first-denomination coin-values))


  (define no-more? null?)
  (define except-first-denomination cdr)
  (define first-denomination car)

Order does not matter, because we just sum amount without each element, and with.

Exercise 2.20

(define (same-parity n . l)
  (define (matches x) 
    (= (remainder n 2) (remainder x 2))
  (define (iter l) 
    (if (null? l)
      (if (matches (car l))
        (cons (car l) (iter (cdr l)))
        (iter (cdr l))
  (cons n (iter l))

Exercise 2.21

(define (sqr x) (* x x))
(define (square-list items)
  (if (null? items)
      (cons (sqr (car items)) (square-list (cdr items)))))

(define (square-list items)
  (map sqr items))

Exercise 2.22

What is cons-ed into list first will be at the end of list.

If we swap arguments to cons, then we will have the same hierarchy (what was consed first is at deepest pair), just link to the next pair is first element of pair, and value is second. To work with such structure properly, we will have to swap car and cdr.

Exercise 2.23

(define (for-each f items)
  (if (null? items)
    ((lambda () ; Need some proper way to write blocks of code in if
      (f (car items))
      (for-each f (cdr items))

(for-each (lambda (x) (newline) (display x))
          (list 57 321 88))