On 8 Nov., 17:47, Phlex <[EMAIL PROTECTED]> wrote:
> Robert Pfeiffer wrote:
> > Hello,
>
> > Is there a benefit in implementing this as a macro instead of a
> > function? The function version would be very simple:
>
> > (defn returning [returnval & body]
> > returnval)
>
> Well no, the forms are evaluated. It's usefull for side effects.
In what way would the forms *not* get evaluated when using
Roberts function returning?
> user> (def *bleh* (ref nil))
> user> (prog1
> (dosync (ref-set *bleh* 3))
> (println (str "bleh is now " @*bleh*)))
> bleh is now 3
> 3
user> (def *bleh* (ref nil))
#=(var user/*bleh*)
user> (returning
(dosync (ref-set *bleh* 3))
(println (str "bleh is now " @*bleh*)))
bleh is now 3
3
> A function would work :
>
> (defn progz [return-value do-also-fn]
> (do-also-fn)
> return-value)
Your function only takes two arguments.
Roberts took an arbitrary number of them.
> user> (progz (+ 1 3)
> (fn []
> (prn "hello")
> (prn "i'm evaluated")))
> "hello"
> "i'm evaluated"
> 4
>
> But it's not as terse.
But this is:
user> (returning (+ 1 3)
(prn "Hello")
(prn "I'm evaluated"))
"Hello"
"I'm evaluated"
4
Macros are much less often needed in a functional language
which also comes with syntactic suger for (λ [args] ...) in form
of #(...).
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---