How to increment by a number in Clojure?

clojure

Solution

Variables are immutable in Clojure. So you should not try to change the value of `foo`, but instead, "create" a new foo:

(def foo2 (+ foo 0.1))

...or, if in a loop, recur with a new value:

(loop [foo 5.0]
  (when (< foo 9)
    (recur (+ foo 0.1))))

...or, if foo is an atom, `swap!` it with a new value:

(def foo (atom 5.0))
(swap! foo (partial + 0.1))

I recommend you start by reading the rationale of Clojure.

Problem

I would like to know how to increment by X amount a number, in other languages I used to do foo += 0.1; but I have not idea how it can be done in Clojure

Original source