3 ;;; Scheme implementation of a `same-fringe' solver. Assumes Chicken, but
4 ;;; should port easily.
6 (use syntax-case) ; Chicken-specfic
8 ;;;--------------------------------------------------------------------------
11 (define-syntax with-values
12 ;; Bind the values returned by FORM to the VARS and evaluate BODY.
15 ((with-values vars form . body)
16 (call-with-values (lambda () form)
18 (apply (lambda vars . body) stuff))))))
21 ;; If CONDITION is not #f then evaluate BODY.
24 ((when condition . body)
25 (if condition (begin . body)))))
28 ;; If CONDITION is #f then evaluate BODY.
31 ((unless condition . body)
32 (if (not condition) (begin . body)))))
34 ;;;--------------------------------------------------------------------------
37 (define-record-type coroutine
38 ;; A coroutine simply remembers the continuaton which was suspended when it
39 ;; last invoked a different coroutine.
40 (make-coroutine continuation)
42 (continuation %coroutine-continuation %set-coroutine-continuation!))
44 (define %current-coroutine (make-coroutine #f))
45 (define (current-coroutine)
46 ;; Return the current coroutine.
49 (define %calling-coroutine #f)
50 (define (calling-coroutine)
51 ;; Return the coroutine that invoked the current one. Before any switch,
55 (define (resume coroutine . args)
56 ;; Switch to COROUTINE, passing it ARGS. When this coroutine is resumed
57 ;; (by calling `switch', naturally) it will return the values passed as
58 ;; arguments. A new coroutine (made by `make-coroutine') receives these
59 ;; values as its arguments.
61 (call-with-current-continuation
63 (%set-coroutine-continuation! %current-coroutine k)
64 (set! %calling-coroutine %current-coroutine)
65 (set! %current-coroutine coroutine)
66 (apply (%coroutine-continuation coroutine) args))))
68 ;;;--------------------------------------------------------------------------
71 (define-syntax define-generator
72 ;; Define a function returning a generator. The generator yields whatever
73 ;; the function body does.
76 ((define-generator (name . args) . body)
78 (make-coroutine (lambda ()
80 (resume (calling-coroutine) #f #f)))))))
82 (define (yield object)
83 ;; Yield OBJECT from a generator. The generator protocol returns two
84 ;; values each time: either an object and #t, or #f twice to mark the end
87 (with-values () (resume (calling-coroutine) object #t) #f))
89 (define (reduce-generator func init gen)
90 ;; Call FUNC for each item in the generator GEN.
92 ;; We maintain a STATE, which is initially INIT. For each ITEM produced by
93 ;; the generator, we replace the state by (FUNC ITEM STATE); finally, we
94 ;; return the final state.
96 (let loop ((state init))
97 (with-values (item any?) (resume gen)
99 (loop (func item state))
102 (define (list-generator gen)
103 ;; Collect the elements generated by GEN into a list and return it.
105 (reverse (reduce-generator cons '() gen)))
107 (define (same-generators? gen-a gen-b)
108 ;; Return whether GEN-A and GEN-B generate the same elements in the same
112 (with-values (a any-a?) (resume gen-a)
113 (with-values (b any-b?) (resume gen-b)
114 (cond ((not any-a?) (not any-b?))
119 ;;;--------------------------------------------------------------------------
122 ;; Assumes SRFI-9; widely available.
123 (define-record-type node
124 ;; A node in a simple binary tree. Empty subtrees are denoted by ().
126 (make-node left data right)
132 (define-generator (fringe node)
133 ;; Generate the elements of the tree headed by NODE inorder.
135 (let recur ((node node))
137 (recur (node-left node))
138 (yield (node-data node))
139 (recur (node-right node)))))
141 (define (parse-tree string)
142 ;; Return a tree constructed according to STRING.
146 ;; tree ::= empty | `(' tree char tree `)'
148 ;; disambiguated by treating `(' as starting a tree wherever a tree is
151 (let ((len (string-length string)))
153 (cond ((>= i len) (values '() i))
154 ((char=? (string-ref string i) #\()
155 (with-values (left i) (parse (+ 1 i))
156 (unless (< i len) (error "no data"))
157 (let ((data (string-ref string i)))
158 (with-values (right i) (parse (+ 1 i))
159 (unless (and (< i len) (char=? (string-ref string i) #\)))
161 (values (make-node left data right) (+ 1 i))))))
162 (else (values '() i))))
163 (with-values (tree i) (parse 0)
164 (unless (= i len) (error "trailing junk"))
167 ;;;--------------------------------------------------------------------------
171 (cond ((null? args) (error "bad args"))
173 (reduce-generator (lambda (ch ?) (write-char ch)) #f
174 (fringe (parse-tree (car args))))
177 (display (if (same-generators? (fringe (parse-tree (car args)))
178 (fringe (parse-tree (cadr args))))
182 (else (error "bad args"))))
184 ;; Chicken-specific (works in interpreter and standalone compiled code).
185 (let ((program (car (argv))))
186 (condition-case (begin (main (command-line-arguments)) (exit))
188 (print-error-message err (current-error-port) program)
191 ;;;----- That's all, folks --------------------------------------------------