tags:

views:

307

answers:

3

What is the idiomatic way to create an infinite loop?


while(true){
   calc();
}

I want to call the calc function forever. Only one function is called over and over again.

EDIT: One other thing I forgot to mention is that calc has side effects. It does some calculations and modifies a byte array.

+4  A: 

(loop [] (calc) (recur))

z5h
This being the syntax for simple tail recursion which I could not immediately bring to mind.
Steve Gilham
+5  A: 

while is in the core libraries.

(while true (calc))

This expands to a simple recur.

(defmacro while
  "Repeatedly executes body while test expression is true. Presumes
  some side-effect will cause test to become false/nil. Returns nil"
  [test & body]
  `(loop []
     (when ~test
       ~@body
       (recur))))
Brian Carper
+4  A: 
Jonas
With while around, it would be kind of pointless to write a forever macro that does the exact same thing. Unless you wanted clarification on the name, and in that case, maybe somebody should write a function synonym macro! :p
Rayne
I don't agree. Do you consider `dosync` pointless? You could just as well write (sync nil ...).
Jonas