Skip to content

Instantly share code, notes, and snippets.

@leque
Last active August 29, 2015 13:56
Show Gist options
  • Save leque/9292681 to your computer and use it in GitHub Desktop.
Save leque/9292681 to your computer and use it in GitHub Desktop.
(define-syntax %cut
(sc-macro-transformer
(lambda (form use-env)
(capture-syntactic-environment
(lambda (mac-env)
(let ((rargs (cadr form))
(rbody (caddr form))
(rest (cdddr form)))
(define (id=? x y)
(and (identifier? x)
(identifier=? use-env x mac-env y)))
(cond ((null? rest)
`(lambda ,(reverse rargs) ,(reverse rbody)))
((and (id=? (car rest) '<...>)
(null? (cdr rest)))
`(lambda (,@(reverse rargs) ,@'r)
(apply ,@(reverse rbody) r)))
((id=? (car rest) '<>)
(let ((arg (make-synthetic-identifier 'arg)))
`(%cut (,arg ,@rargs) (,arg ,@rbody) ,@(cdr rest))))
(else
`(%cut ,rargs (,(car rest) ,@rbody) ,@(cdr rest))))))))))
(define-syntax cut
(sc-macro-transformer
(lambda (form use-env)
`(%cut () () ,@(map (lambda (x)
(close-syntax x use-env))
(cdr form))))))
;; ----------
(write ((cut list 1 3 5 <> <>) 4 9))
(newline)
(write ((cut list 1 3 5 <...>) 4 9 8))
(newline)
(let ((<> 1))
(write ((cut list 0 <> <...>) 2 3))
(newline))
(let ((<...> 1))
(write ((cut list 0 <...> <>) 2))
(newline))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment