Numbers in Clojure include:
-6
, 0
, 1
, 25
, 976
and 500000
.-2.4
, 0.1
, 3.14
, 16.984025
and 1024.0
.Two common numeric types are int
and float
. An int
is a 32-bit integer and a float
is a 64-bit floating-point number.
Arithmetic is done using the standard arithmetic operators. Numbers can be compared using the standard numeric comparison operators (>
, <
, <=
, >=
), the equality operator (=
) and the non-equality operator (not=
).
In this exercise you must conditionally execute logic. A common way to do this in Clojure is by using cond
:
(cond (= x 5) "Expression to evaluate when x equals 5"
(> x 7) "Expression to evaluate when x is greater than 7"
:else "Expression to evaluate in all other cases")
The ==
operator might be preferable to =
in some cases where numbers need to be compared irrespective of the exact type. For instance,
(== 5.0 5) ;; true as both numbers are equal when type is ignored
(= 5.0 5) ;; false as the types of numbers are also taken into account here i.e. float is different from int
The Clojure guide on Equality, specifically the section on Numbers goes into more details.
In this exercise you'll be writing code to analyze the production of an assembly line in a car factory. The assembly line's speed can range from 0
(off) to 10
(maximum).
At its lowest speed (1
), 221
cars are produced each hour. The production increases linearly with the speed. So with the speed set to 4
, it should produce 4 * 221 = 884
cars per hour. However, higher speeds increase the likelihood that faulty cars are produced, which then have to be discarded. The following table shows how speed influences the success rate:
0
: 0% success rate.1
to 4
: 100% success rate.5
to 8
: 90% success rate.9
: 80% success rate.10
: 77% success rate.You have two tasks.
Implement the production-rate
function to calculate the assembly line's production rate per hour, taking into account its success rate:
(production-rate 6)
;;=> 1193.4
Note that the value returned is a double
.
Implement the working-items
function to calculate how many working cars are produced per minute:
(working-items 6)
;;=> 19
Note that the value returned is an int
.
Sign up to Exercism to learn and master Clojure with 12 concepts, 85 exercises, and real human mentoring, all for free.