[ prog / sol / mona ]

prog


Lisp beginner thread

40 2020-05-30 02:47

>>39
I've been working through Section 2.1 today (a day behind schedule due to some difficult problems in 1.2, 3 of which remain unsolved) and I believe it answered most of my question. It seems the authors only used the enumeration in their version because they hadn't introduced the idea of compound data yet. I still need to reflect and digest the concept of data-abstraction some more, I could clearly define constructors and selectors for denomination sets, but the utility is not completely clear to me in this case. I ended up memoizing the process, just for fun. Data abstraction does seem like it would be very useful for the generic memoization procedure, so you could easily replace the list with something with less egregiously slow search times, and even customize it to the data being searched for in cases where that's more important than simplicity.

(define (find n N R)
  (let F ((N N) (R R))
    (cond ((null? N) #f)
          ((equal? n (car N)) (car R))
          (else (F (cdr N) (cdr R))))))

(define (memo-change a)
  (define cc
    (let ((input '()) (output '()))
      (lambda (a n)
        (or (find (cons a n) input output)
            (let ((result (C a n)))
              (set! input (cons (cons a n) input))
              (set! output (cons result output))
              result)))))
  (define (C a n)
     (+ (cond ((= a (car n)) 1)
              ((> a (car n)) (cc (- a (car n)) n))
              (else 0))
        (cond ((= (cadr n) 1) 1)
              (else (cc a (cdr n))))))
  (C a '(50 25 10 5 1)))
132


VIP:

do not edit these