[Top][All Lists]

[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


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?

; 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))
        (lambda (,+sk)
          ,@(map (lambda (alt)
                     (lambda (,+ck)
                       (set! m-fail
                             (lambda ()
                               (set! m-fail ,+mf)
                               (,+ck 'fail)))
                       (,+sk ,alt))))

(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
                   (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))

                   (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*)



reply via email to

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