exercises.org 4.0 KB

Exercise 2.17

  (define (last-pair mylist)
    (if (null? (cdr mylist))
        mylist
        (last-pair (cdr mylist))))
  (last-pair (list 5 4 3 2))
2

Exercise 2.18

  (define (reverse mylist)
    (define (reverser backwards original)
      (if (null? original)
          backwards
          (reverser (cons (car original) backwards) (cdr original))))
    (reverser (list) mylist))
  (reverse (list 1 2 3 4))
4 3 2 1

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)
          (else
           (+ (cc amount
                  (except-first-denomination coin-values))
              (cc (- amount
                     (first-denomination coin-values))
                  coin-values)))))

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

  (cc 100 uk-coins)
104561

Exercise 2.20

my bday!!

  (define (same-parity a . ns)
    (define (pare a ns)
      (cond ((null? ns)
             (list))
            ((= (remainder (car ns) 2) (remainder a 2))
             (cons (car ns) (pare a (cdr ns))))
            (else (pare a (cdr ns)))))
    (pare a ns))
  
  (same-parity 1 2 3 4 5 6 7 8 9)
3 5 7 9

Exercise 2.21

  (define (square-list1 items)
    (if (null? items)
	(list)
	(cons (square (car items)) (square-list1 (cdr items)))))

  (define (square-list2 items)
    (map (lambda (x) (square x)) items))

  (square-list1 (list 1 2 3 4 5))
  (square-list2 (list 1 2 3 4 5))

Exercise 2.24

i think louis' problem comes from the fact that the recursion goes to the base case before any action is taken, giving reverse order bc the last element is processed first. i think.

Exercise 2.23

  (define (my-for-each proc items)
    (cond ((null? items)
	   0)
	  (else
	   (proc (car items))
	   (my-for-each proc (cdr items)))))

  (my-for-each (lambda (x) (newline) (display x)) (list 398 24 344))

Exercise 2.24

(list 1 (list 2 (list 3 4)))
-> (1 (2 (3 4)))
-> 1|-->2|-->3|-->4|\

Exercise 2.25

  (define a (list 1 3 (list 7 5) 9))
  (car (caddr a))

  (define b (list (list 7)))
  (caar b)

  (define c (list 1 (list 2 (list 3 (list 4 (list 5 (list 6 7)))))))
  (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr (car (cdr c))))))))))))
  ;; cadadadadadadr? lol

Exercise 2.26

  (define x (list 1 2 3))
  (define y (list 4 5 6))

  (append x y)
  ;; (1 2 3 4 5 6)

  (cons x y)
  ;; ((1 2 3) 4 5 6)

  (list x y)
  ;; ((1 2 3) (4 5 6))

Exercise 2.27

  (define x (list (list 1 2) (list 3 4)))

  x
  ;; ((1 2) (3 4))


  (define (reverse mylist)
    (define (reverser backwards original)
      (if (null? original)
	  backwards
	  (reverser (cons (car original) backwards) (cdr original))))
    (reverser (list) mylist))

  (reverse x)
  ;; ((3 4) (1 2))

  (define (deep-reverse x)
  )
  ;; TODO ... idk
  (deep-reverse x)

Exercise 2.28

  (define x (list (list 1 2) (list 3 4)))

  (define (fringe x)
    (if (not (pair? (car (cdr x))))
	(cons 
	(cons (fringe (car x)) (fringe (cdr x)))))


  (fringe x)

  ;; TODO
  ;; again, idk.. breaking my brain

Exercise 2.42

the 8 queens puzzle

  (define (queens board-size)
    (define (queen-cols k)
      (if (= k 0)
          (list empty-board)
          (filter
           (lambda (positions) (safe? k positions))
           (flatmap
            (lambda (rest-of-queens)
              (map (lambda (new-row)
                     (adjoin-position new-row k rest-of-squares))
                   (enumerate-interval 1 board-size)))
            (queen-cols (- k 1))))))
    (queen-cols board-size))