How to increase the number in Clojure?

I would like to know how to increase the number of numbers by X, in other languages ​​that I used

foo + = 0.1;

but I don’t know how to do this in Clojure

+5
source share
2 answers

Variables are immutable in Clojure. Therefore, you should not try to change the value 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 has a new meaning:

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

I recommend that you start by reading the rationale for Clojure .

+8
source

Blacksad , , , , , :

:

user> (defn my-function [x]
        (let [y (inc x)
              z (+ x y)]
          [x y z]))
#'user/my-function
user> (my-function 4)
[4 5 9]

, :

user> (defn my-function [x]
        (let [y (inc x)
               z (+ x y)
               z (+ z 4)
               z (* z z)]
          [x y z]))
#'user/my-function
user> (my-function 4)
[4 5 169]

, " " . clojure.core threading.

+3

All Articles