←back to thread

88 points azhenley | 2 comments | | HN request time: 0.398s | source
Show context
adityaathalye ◴[] No.45155326[source]
In Clojure...

  ((fn [xs ret]
     (if (empty? xs)
       ret
       (recur (rest xs)
              (+ ret (first xs)))))
   (range 5) 0)

  => 10
nb. Clojure doesn't have automatic tail call optimisation. We need to explicitly emulate it with`recur`.
replies(2): >>45156609 #>>45161445 #
1. skrishnamurthi ◴[] No.45161445[source]
It's not the same thing. `recur` in Clojure must be in tail-position. This program

https://news.ycombinator.com/item?id=45154253

would therefore not work.

replies(1): >>45161649 #
2. adityaathalye ◴[] No.45161649[source]
I was trying to say something like that with my note in the GP comment:

  > "nb. Clojure doesn't have automatic tail call optimisation. We need to explicitly emulate it with`recur`."
Just an average joe programmer here... advanced macrology is way above my pay grade :sweat-smile:.