Skip to content

Instantly share code, notes, and snippets.

@jkominek
Last active August 29, 2015 14:14
Show Gist options
  • Save jkominek/1dbd66511b886dbd5122 to your computer and use it in GitHub Desktop.
Save jkominek/1dbd66511b886dbd5122 to your computer and use it in GitHub Desktop.
simple syntax-parse macros to rearrange evaluation
#lang racket
(require (for-syntax syntax/parse)
syntax/parse)
; why would you want infix syntax, when you can
; change your syntax to suit the task at hand?
(define-syntax ($ stx)
(syntax-parse stx
#:datum-literals ($)
[($ v) #'v]
[($ v (f a ... $ b ...))
#'(f a ... v b ...)]
[($ v (f a ...))
#'(f v a ...)]
[($ v (f a ...) ... (g b ... $ c ...))
#'(g b ... ($ v (f a ...) ...) c ...)]
[($ v (f a ...) ... (g b ...))
#'(g ($ v (f a ...) ...) b ...)]
))
(define-syntax (<$ stx)
(syntax-parse stx
#:datum-literals ($)
[(<$ v) #'v]
[(<$ (f a ... $ b ...) v)
#'(f a ... v b ...)]
[(<$ (f a ...) v)
#'(f v a ...)]
[(<$ (g b ... $ c ...) (f a ...) ... v)
#'(g b ... (<$ (f a ...) ... v) c ...)]
[(<$ (g b ...) (f a ...) ... v)
#'(g (<$ (f a ...) ... v) b ...)]
))
($ '(1 5 9) (list-ref 1) (+ 2) (/ 2 $))
(<$ (/ 2 $) (+ 2) (list-ref 1) '(1 5 9))
; improvement: find the $ marker anywhere, so that
; ($ 5 (if (= 5 $) #t #f))
; could work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment