chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Problem with amb


From: Brown Dragon
Subject: [Chicken-users] Problem with amb
Date: Thu, 30 Oct 2008 17:45:10 +0530

Hello,

I am learning scheme and am trying to implement the "amb" operator.

The version I have implemented works fine in Gambit-C but does not work in Chicken. Could someone tell me what's wrong?

<code>
; Defining the amb operator (from Teach yourself Scheme in Fixum days)
(define call/cc call-with-current-continuation)
(define-macro (amb . alts)
  (let ((+mf (gensym))(+sk (gensym))(+ck (gensym)))
    `(let ((,+mf m-fail))
       (call/cc
        (lambda (,+sk)
          ,@(map (lambda (alt)
                   `(call/cc
                     (lambda (,+ck)
                       (set! m-fail
                             (lambda ()
                               (set! m-fail ,+mf)
                               (,+ck 'fail)))
                       (,+sk ,alt))))
                 alts)
          (,+mf))))))

(define (m-fail) (error "Program failed!"))

; Ok. Let's set up a few structures for the problem.
; For Gambit-C undefine the following
;(define-structure house nationality color drinks smokes pet)
; For Chicken uncomment the following
(define-record house nationality color drinks smokes pet)
(define (show-house h)
  (pretty-print "House:")
  (pretty-print (house-nationality h))
  (pretty-print (house-color h))
  (pretty-print (house-drinks h))
  (pretty-print (house-smokes h))
  (pretty-print (house-pet h)))
; Now let us pick the values of the houses on the street
(set! *street* (list
      (make-house
                   (amb 'Brit 'Swede 'Dane 'Norwegian 'German)
                   (amb 'Red 'Green 'White 'Blue 'Yellow)
                   (amb 'Tea 'Milk 'Coffee 'Beer 'Water)
                   (amb 'PallMall 'Dunhill 'Marlboro 'Winfield 'Rothmans)
                   (amb 'Dogs 'Cats 'Horses 'Birds 'Fishes))

      (make-house
                   (amb 'Brit 'Swede 'Dane 'Norwegian 'German)
                   (amb 'Red 'Green 'White 'Blue 'Yellow)
                   (amb 'Tea 'Milk 'Coffee 'Beer 'Water)
                   (amb 'PallMall 'Dunhill 'Marlboro 'Winfield 'Rothmans)
                   (amb 'Dogs 'Cats 'Horses 'Birds 'Fishes))))

;No two owners have the same nationality or pet, smoke the same brand or drink the same beverage.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FAILS HERE - should iterate till it picks Brit, Swede
;; Instead stops at first iteration (changes dog to cat)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(if (eq? (house-nationality (car *street*))
         (house-nationality (cadr *street*))) (amb))

;And print out the result!
(map (lambda (h) (show-house h)) *street*)

</code>

cheers!
/BD

reply via email to

[Prev in Thread] Current Thread [Next in Thread]