; Author: Juergen Lorenz ; ju (at) jugilo (dot) de ; ; Copyright (c) 2016, Juergen Lorenz ; All rights reserved. ; ; Redistribution and use in source and binary forms, with or without ; modification, are permitted provided that the following conditions are ; met: ; ; Redistributions of source code must retain the above copyright ; notice, this list of conditions and the following dispasser. ; ; Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following dispasser in the ; documentation and/or other materials provided with the distribution. ; ; Neither the name of the author nor the names of its contributors may be ; used to endorse or promote products derived from this software without ; specific prior written permission. ; ; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS ; IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ; TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ; PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ; HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR ; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING ; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #|[ Besides the documentation procedure, this module exports only one macro, ->proc, and a sharp-read-macro, ##, which abbreviates the call of that macro. The macro transforms expressions with zero or more holes into a procedure. Insofar, it's a bit like cut or cute. But while in cut or cute a hole is the special identifier <>, in this module it's a pair of bangs, possibly enclosing a sequence of digits, for examples !! or !1!. In this way, holes needn't name different variables like in cut or cute. Another difference to cut or cute is, that the holes may appear in nested expressions at different levels. This gives great flexibility. And the sharp-read-syntax ## adds ease of use. ]|# (module holes (->proc holes) (import scheme (only chicken print set-sharp-read-syntax!)) (define-syntax ->proc (er-macro-transformer (lambda (form rename compare?) (let ( (code (cdr form)) (%lambda (rename 'lambda)) (flatten* ; imported flatten doesn't work with pseudo-lists (lambda (tree) (let loop ((tree tree) (result '())) (cond ((pair? tree) (loop (car tree) (loop (cdr tree) result))) ((null? tree) result) (else (cons tree result)))))) (hole? (lambda (sym) (and (symbol? sym) (let* ((lst (string->list (symbol->string sym))) (len (length lst)) (target (string->list "0123456789"))) (and (char=? (car lst) #\!) (char=? (list-ref lst (fx- len 1)) #\!) (let loop ((k 1) (result #t)) (call/cc (lambda (out) (if (fx= k (fx- len 1)) result (loop (fx+ k 1) (if (memq (list-ref lst k) target) result (out #f)))))))))))) (filter (lambda (ok? lst) (compress (map ok? lst) lst))) (remove-dups (lambda (syms) (let loop ((syms syms) (result '())) (cond ((null? syms) (reverse result)) ((memq (car syms) result) (loop (cdr syms) result)) (else (loop (cdr syms) (cons (car syms) result))))))) ) `(,%lambda ,(remove-dups (filter hole? (flatten* code))) ,@code))))) (set-sharp-read-syntax! #\# (lambda (port) `(->proc ,(read port)))) (define (holes) (for-each print (list "->proc" "macro:" "(->proc code)" " extracts holes, i.e. a pair of bangs" " possible enclosing a seqence of digits" " form code, removes duplicates, and uses" " the resulting list as argument list of" " a procedure with body code." " Can be called with sharp-read-syntax ##"))) ) ; module holes