; 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. ]|# (require-library irregex) (module holes (->proc holes) (import scheme (only chicken print set-sharp-read-syntax!)) (import-for-syntax (only chicken foldr) (only irregex sre->irregex irregex-extract) (only data-structures ->string)) (define-syntax ->proc (er-macro-transformer (lambda (form rename compare?) (let ((code (cdr form)) (%lambda (rename 'lambda))) (let ((irx (sre->irregex '(: (or space "(") "!" (* (/ "09")) "!" (or space ")")))) ; (foldr (lambda (op acc lst) ; (let loop ((lst lst)) ; (if (null? lst) ; acc ; (op (car lst) ; (loop (cdr lst))))))) (adjoin (lambda (sym syms) (if (memq sym syms) syms (cons sym syms)))) ) `(,%lambda ,(foldr adjoin '() ; remove dups (map string->symbol (map (lambda (str) (substring str 1 (- (string-length str) 1))) (irregex-extract irx (->string code))))) ,@code)))))) (set-sharp-read-syntax! #\# (lambda (port) `(->proc ,(read port)))) (define (holes) (for-each print (list "->proc" "macro:" "(->proc xpr)" " extracts holes, i.e. a pair of bangs" " possible enclosing a seqence of digits" " form xpr, removes duplicates, and uses" " the resulting list as arbument list of" " a procedure with body xpr" " can be called with sharp-read-syntax ##"))) ) ; module holes