; 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!)) (import-for-syntax (only chicken receive)) (define-syntax ->proc (er-macro-transformer (lambda (form rename compare?) (let ( (code (cadr 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))) (ninsert (lambda (n lon) (let loop ((lon lon)) (cond ((null? lon) (list n)) ((= n (car lon)) lon) ((< n (car lon)) (cons n lon)) ((> n (car lon)) (cons (car lon) (loop (cdr lon)))))))) (split-with (lambda (where? lst) (let loop ((tail lst) (head '())) (if (or (null? tail) (where? (car tail))) (values (reverse head) tail) (loop (cdr tail) (cons (car tail) head)))))) (no-dups? (lambda (lst) (let loop ((lst lst) (result '())) (cond ((null? lst) #t) ((memq (car lst) result) #f) (else (loop (cdr lst) (cons (car lst) result))))))) ) (let* ( (nsort (lambda (lon) (let loop ((lon lon) (result '())) (if (null? lon) result (loop (cdr lon) (ninsert (car lon) result)))))) (hsort (lambda (holes) (let* ( (strings (map symbol->string holes)) (substrings (map (lambda (s) (substring s 1 (fx- (string-length s) 1))) strings)) (nums (map string->number (filter (lambda (s) (not (string=? s ""))) substrings))) (snums (nsort nums)) (sstrings (map (lambda (s) (string-append "!" s "!")) (map number->string snums))) (sholes (map string->symbol sstrings)) ) (if (memq '!! holes) (cons '!! sholes) sholes)))) ) (if (and (list? code) (let ((lst (split-with (lambda (x) (not (symbol? x))) code))) (and (memq ': lst) (no-dups? lst)))) (receive (head tail) (split-with (lambda (x) (eq? x ':)) code) `(,%lambda ,head ,@(cdr tail))) `(,%lambda ,(hsort (filter hole? (flatten* code))) ,code))))))) (set-sharp-read-syntax! #\# (lambda (port) `(->proc ,(read port)))) (define (holes) (for-each print (list "->proc" "macro:" "(->proc code)" " either:" " extracts holes, i.e. a pair of bangs" " possible enclosing a seqence of digits" " from code, sorts them numerically while" " removing duplicates, and uses" " the resulting list as argument list of" " a procedure with body code." " or:" " searches code for a colon and treats the symbols" " to the left as argument-list of a procedure with" " body the expressions to the right." " Can be called with sharp-read-syntax ##" " Note, that ##code is always a procedure!"))) ) ; module holes