[ prog / sol / mona ]

prog


Post macros.

30 2020-07-06 20:08
(define-syntax let1*
  (syntax-rules ()
    ((_ (((var0 val0) proc0 ...)) body ...)
     ((lambda (var0) proc0 ... body ...) val0))
    ((_ (((var0 val0) proc0 ...) ((var1 val1) proc1 ...) ...) body ...)
     ((lambda (var0)
        proc0 ...
        (let1* (((var1 val1) proc1 ...) ...) body ...))
      val0))))

(define (multiple-dwellings)
  (let1* (((fletcher (amb 1 2 3 4 5))
           (require (not (= fletcher 1)))
           (require (not (= fletcher 5))))
          ((cooper (amb 1 2 3 4 5))
           (require (not (= cooper 1)))
           (require (not (= (abs (- fletcher cooper)) 1)))
           (require (not-member cooper (list fletcher))))
          ((smith (amb 1 2 3 4 5))
           (require (not (= (abs (- smith fletcher)) 1)))
           (require (not-member smith (list fletcher smith))))
          ((miller (amb 1 2 3 4 5))
           (require (> miller cooper))
           (require (not-member miller (list smith cooper fletcher))))
          ((baker (amb 1 2 3 4 5))
           (require (not (= baker 5)))
           (require (not-member baker (list miller smith cooper fletcher)))))
    `((baker ,baker)
      (cooper ,cooper)
      (fletcher ,fletcher)
      (miller ,miller)
      (smith ,smith))))

I wrote the attached macro today to remove some ugly lateral movement while optimizing the ‘multiple-dwelling’ procedure from SICP. If I was going to be writing in this non-deterministic language frequently I think I would change the interface a good bit (just look at that nasty repetition of ‘non-member’!), but this helps a good bit.

40


VIP:

do not edit these