Values & arithmetic
Numbers work the way you expect — with one twist that makes them nicer.
Start small
Section titled “Start small”(+ 1 2)Output:
3+ is a function, so it goes first. The bonus: it takes any number of
arguments.
(* 2 3 4)Output:
24Comparisons chain
Section titled “Comparisons chain”In most languages you write 1 < 2 && 2 < 3. Here you just list the
numbers:
(< 1 2 3)Output:
true(< 1 2 3) asks “is this sequence increasing?” — three numbers, one call.
Division that doesn’t lie
Section titled “Division that doesn’t lie”Integer division and remainder are separate, honest functions:
(quot 17 5)(rem 17 5)Output:
32Put it together: average a shopping list
Section titled “Put it together: average a shopping list”(def prices [120 45 80])
(reduce + prices)Output:
245reduce feeds the vector through + one element at a time —
120 + 45 + 80.
(/ (reduce + prices) (count prices))Output:
245/3That 245/3 is a ratio — cljgo keeps the exact fraction instead of
silently rounding. Ask for a decimal only when you actually want one:
(double (/ (reduce + prices) (count prices)))Output:
81.66666666666667Exactness by default, rounding by choice.
Questions or feedback on this page? Comment below with your GitHub account — comments are public and live in the project's GitHub Discussions.