* Exercise 2.17
#+BEGIN_SRC scheme
  (define (last-pair mylist)
    (if (null? (cdr mylist))
        mylist
        (last-pair (cdr mylist))))
  (last-pair (list 5 4 3 2))
#+END_SRC

#+RESULTS:
| 2 |

* Exercise 2.18
#+BEGIN_SRC scheme
  (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))
#+END_SRC

#+RESULTS:
| 4 | 3 | 2 | 1 |

* Exercise 2.19
#+BEGIN_SRC scheme
  (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)
#+END_SRC

#+RESULTS:
: 104561

* Exercise 2.20
my bday!!
#+BEGIN_SRC scheme
  (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)
#+END_SRC

#+RESULTS:
| 3 | 5 | 7 | 9 |
* Exercise 2.21
#+BEGIN_SRC scheme
  (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))
#+END_SRC
* 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
#+BEGIN_SRC scheme
  (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))
#+END_SRC
* Exercise 2.24

: (list 1 (list 2 (list 3 4)))

: -> (1 (2 (3 4)))

: -> 1|-->2|-->3|-->4|\
* Exercise 2.25

#+BEGIN_SRC scheme
  (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
#+END_SRC

* Exercise 2.26

#+BEGIN_SRC scheme
  (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))
#+END_SRC

* Exercise 2.27
#+BEGIN_SRC scheme
  (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)
	
#+END_SRC

* Exercise 2.28
#+BEGIN_SRC scheme
  (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
#+END_SRC

* Exercise 2.42
the 8 queens puzzle

#+BEGIN_SRC scheme
  (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))
#+END_SRC
