Skip to content

Instantly share code, notes, and snippets.

@travis
Forked from sritchie/macro.md
Created January 25, 2012 14:36
Show Gist options
  • Save travis/1676543 to your computer and use it in GitHub Desktop.
Save travis/1676543 to your computer and use it in GitHub Desktop.

A little Clojure challenge inspired by Let over Lambda.

Write a macro (you can try a function, but it's impossible) that accepts four arguments:

  1. an expression that returns a number
  2. something to return if that number's negative
  3. something to return if that number's zero
  4. something to return if that number's positive

Here's the signature: (defmacro nif [expr neg zero pos] ...)

To pass my test, the following form should print "pos!" and return "pos.", with no other side effects:

(nif 10
      (do (println "Negative!") "negative.")
      (do (Thread/sleep 1000000) "zero.")
      (do (println "pos!") "pos."))

May the best lisper win!

@sritchie
Copy link

:else is just a convention, since keywords are always truthy. This is equally valid, but unclear:

(cond (pos? x) "pos"
      (neg? x) "neg"
      "FALSE!" "zero")

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