Skip to content

Instantly share code, notes, and snippets.

@rentalcustard
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rentalcustard/d83cf7bd4805512c90d6 to your computer and use it in GitHub Desktop.
Save rentalcustard/d83cf7bd4805512c90d6 to your computer and use it in GitHub Desktop.
(defmacro new-if [pred then-clause else-clause]
`(cond ~pred ~then-clause
:else ~else-clause))
"This works, in that if I call it like so:
"
(new-if (= 2 3) (while true (print "hi")) :bye)
"I don't get an infinite loop, but I don't understand how. I've unquoted
everything in the macro body, so shouldn't the normal evaluation order
apply? Why doesn't it evaluate then-clause when I do it this way?
"
@rentalcustard
Copy link
Author

So it turns out that cond is already doing everything I want new-if to do. I want to demonstrate the power of macros by defining 'if' with one, but I can't work out how to do it without using a pre-existing macro?

@puredanger
Copy link

"if" is a special form with custom evaluation order. Usually people write macros in terms of "if" (or other things built on if like cond) to show how to write new syntax with delayed evaluation.

@rentalcustard
Copy link
Author

I think I'll have more luck doing it with Scheme's define-syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment