Take the 2-minute tour ×

I'd like to change the syntax of the following expression:

(> 2 1)

to something like:

(2 greater 1)

My first try is the following macro:

(define-syntax greater 
  (lambda (x)
    (syntax-case x (greater)
      [(a greater b)
       (syntax (> a b))])))

Using this macro fails with: "bad syntax in: greater"

I've been surfing some Scheme docs, but I was not able to find the way to do it.

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

The expression (2 greater 1) is an application. It expands to (#%app 2 greater 1). You must define your own version of #%app and call it, say, my-%app. If greater is present swap the first and second argument, otherwise just expand to the standard #%app.

To use your new application you must export it from the file (module) in which you define it, and then import it in the module where your want your special application syntax.

share|improve this answer
See: github.com/dyoo/infix-syntax-example for an example of one way to do Jens's suggestion. I've tried to document the source code well, so I'd appreciate any suggestions. –  dyoo Oct 4 '12 at 18:08
add comment

In Racket, there already exists a reader feature to allow for general infix notation: write a dot before and after the function or macro name: (2 . > . 1) It's a little verbose (the dots have to be surrounded by spaces), but I like it and use it a lot. See the documentation for more information.

share|improve this answer
I think this is a better solution. It is clear from the syntax whether it is the usual prefix notation or a special infix operator. Furthermore, (sort some-list <) will still work –  ReyCharles Oct 4 '12 at 18:48
add comment

You might that the "curly-infix" notation is what you want. Just surround a list with {...}, and you can write the list contents in infix order instead of prefix order (the reader transforms it). So if you write {x + ,y} the reader maps it to (+ x ,y).

Curly-infix is defined in SRFI-105: http://srfi.schemers.org/srfi-105/

I know that the current version of GNU guile, at least, implements it.

The "sweet-expression" notation of SRFI-110 ( http://srfi.schemers.org/srfi-110/ ) builds on top of SRFI-105.

share|improve this answer
add comment

Your Answer


