<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br></div><div>Sean, it took me a while to figure out condlet but here is how a Racketeer would write this (questionable) macro: </div><div><br></div><div><div>(define-syntax (condlet stx)</div><div> (syntax-case stx ()</div><div> [(condlet ((c (x e) ...) ...) body ...)</div><div> #'(cond</div><div> [c (let* ((x '()) ... ...)</div><div> (let ((x e) ...)</div><div> body ...))] </div><div> ...)]))</div></div><div><br></div><div>As Stephan points out, a let* suffices here because it simply doesn't matter because it simply doesn't matter which x binding body ... sees. </div><div><br></div><div>For your information, you can also write </div><div><br></div><div><div>(define-syntax (condlet stx)</div><div> (syntax-case stx ()</div><div> [(condlet ((c (x e) ...) ...) body ...)</div><div> (let* ((vars (remove-duplicates #'(x ... ...)))</div><div> (nils (map (lambda (c) #''()) vars)))</div><div> #`(cond</div><div> [c ((lambda #,vars</div><div> (let ((x e) ...)</div><div> body ...))</div><div> #,@nils)]</div><div> ...))]))</div><div><br></div><div>(define-for-syntax (remove-duplicates vars)</div><div> (let loop ((vars (syntax->list vars)) (seen '()))</div><div> (cond</div><div> [(null? vars) seen]</div><div> [(memf (lambda (s) (free-identifier=? (car vars) s)) seen) (loop (cdr vars) seen)]</div><div> [else (loop (cdr vars) (cons (car vars) seen))])))</div></div><div><br></div><div>The define-for-syntax form introduces functions usable during the syntax phase. That keeps your syntax definitions small. See style guide. </div><div><br></div><div>Here are the tests I ran </div><div><br></div><div><div>(module+ test</div><div> (require rackunit)</div><div> (define (princ s)</div><div> (displayln s)</div><div> s))</div><div><br></div><div>(module+ test </div><div> </div><div> (check-equal? </div><div> (let ((x 1))</div><div> (condlet (((= x 2) (x (princ 'a)) (y (princ 'b)))</div><div> ((= x 1) (y (princ 'c)) (x (princ 'd)))</div><div> (else (x (princ 'e)) (z (princ 'f))))</div><div> (list x y z)))</div><div> (list 'd 'c '())))</div></div><div><br></div><div><br></div><div>-- Matthias</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><br><div><div>On Jan 1, 2013, at 5:59 PM, Sean Kanaley wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">While I've ultimately succeeded in having it return the correct output for a sample input, I'm not positive it's correct in general and I <i>am </i>positive it's written poorly, as I don't fully understand both syntax-case and syntax objects vs. datums. If someone could look it over and provide a more canonical version, I would be grateful.<br>
<br>Here's how the macro works:<br><br><span style="font-family:courier new,monospace">> (condlet (((= 1 2) (x (princ ’a)) (y (princ ’b)))<br> ((= 1 1) (y (princ ’c)) (x (princ ’d)))<br> (t (x (princ ’e)) (z (princ ’f))))<br>
(list x y z))<br><b>CD<br>(D C NIL)</b></span><br><br>Before I post the horrible racket code, I will explain the problems I'm having with macros in general:<br><br>Problem 1, separate phases: I have a remove-duplicates-by function that would be great to have globally, but it seemingly must be written locally.<br>
<br>Problem 2: You can't use a pattern variable outside of a pattern, so
you have to syntax-ify it with #', but then you can't access the associated
s-exp without removing the syntax. The way to bind things to null by default is to get every id and output the obvious let statement, except ids might be repeated so you have to remove duplicates (enter problem 1). It's remove-duplicates-BY because the removal happens by syntax->datum'ing each identifier-syntax-thing since it can't appear outside of a pattern.<br>
<br>Problem 2: How to remove a portion of the macro code into a separate transformer function? It's kind of annoying having a whole block of code relegated to cleaning up the duplicate ids inside of the let it expands into. That would ideally be written "let #,(remove-dups #'(c cs...))" or similar...some kind of sub-macro to handle just getting ids. I thought that's what let-syntax or nested define syntaxes were for but I get phase errors or preposterous, very dark errors like "lambda not bound". Suddenly I prefer Haskell's <b>a is not an infinitely existential StateT (Bool -> IO (StateT (Cont String) (Cont String) ())) Maybe (a1,a1'), in subexpression f . g</b>. Oh, f <$> g. <b>everything checks out now! </b>Thanks, ghci, and by the way go **** yourself you stupid Cont.<br>
<br>Anywayyyyy here is my code that works for the above example at least:<br><br><span style="font-family:courier new,monospace">(define-syntax (condlet s)<br> (let ((remove-duplicates-by<br> (λ (f l) (let R ((l l))<br>
(if (null? l)<br> null<br> (cons (car l) (R (remove* (list (car l))<br> (cdr l)<br> (λ (a b)<br>
(eq? (f a) (f b)))))))))))<br> (syntax-case s ()<br> ((_ (c) body ...)<br> (syntax-case #'c (else)<br> ((else binds ...)<br> #'(let (binds ...) body ...))<br>
((t binds ...)<br> #'(if t (let (binds ...) body ...) (void)))))<br> ((_ (c cs ...) body ...)<br> (syntax-case #'c ()<br> ((t binds ...)<br> #`(let #,(syntax-case #'(c cs ...) ()<br>
(((_ (i _) ...) ...)<br> (map (λ (i) #`(#,i null))<br> (remove-duplicates-by<br> syntax->datum<br> (syntax->list #'(i ... ...))))))<br>
(if t<br> (let (binds ...) body ...)<br> (condlet (cs ...) body ...)))))))))</span><br><br>Any help is appreciated.<br>
____________________<br> Racket Users list:<br> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>