Logic
Execute code conditionally:
Run alternative code if a condition fails:
Use if as an expression:
An if expression with no else branch returns nil if the condition fails:
Check for equality and inequality:
Compare numbers:
Unlike infix operators, prefix operators allow for many operands:
Use type predicate functions to check whether or not an expression is of a certain type:
Use logical expressions to combine conditions:
Only the values false and nil are falsy and evaluate to false in a
condition. All other values, including the number zero, the empty string, and
empty collections are truthy and evaluate to true.
Group expressions to blocks:
Execute a block conditionally:
Handle multiple conditions:
Compare an expression against multiple values:
Catch an exception:
Throw an exception:
Exceptions thrown by ex-info can be caught as clojure.llang.ExceptionInfo:
Exercises
Range Check
Write a function in-range? that returns true when the argument x is within
the interval of parameters a (lower limit) and b (upper limit), and false
otherwise.
Hint: Use two conditions in conjunction—or a comparison operator using three operands.
Test: (in-range? 3 1 5) shall return true, (in-range 17 1 10) shall return
false.
Pythagorean Triplet
Write a function pythagorean-triplet that accepts three numbers a, b, and
c. If the condition a²+b²=c² holds true for the arguments provided, the
string a²+b²=c² shall be returned, with a, b, and c replaced by their
actual values. If the condition does not hold, the string a²+b²≠c² shall be
returned with the according replacements.
Hint: Use Math/pow to compute the exponents, and str to concatenate the
return value.
Test: (pythagorean-triplet 3 4 5) shall return 3²+4²=5², and
(pythagorean-triplet 1 2 3) shall return 1²+2²≠3².
Quadratic Formula
Write a function quadratic that accepts parameters a, b, and c, and
returns the solutions to equations of the form ax²+bx+c=0 using the quadratic
formula. The function shall return a vector of zero, one, or two elements.
Hint: Use cond and write a helper function discriminant. To turn x into a
negative number, write (- x). Use :else instead of a floating point
comparison against 0.0.
Test: (quadratic 1 -4 3) shall return [3.0 1.0], (quadratic 1 2 1) shall
return [-1], and (quadratic 1 4 5) shall return [].
State Machine
Write a function next-editor-state that accepts two parameters state
(:clean-unsaved, :dirty-unsaved, :clean-saved, :dirty-saved) and
action (:edit, :save), and returns the new state based on the following
transitions:
- A clean unsaved state becomes dirty unsaved when edited and clean saved when saved.
- A dirty unsaved state remains when edited and becomes clean saved when saved.
- A clean saved state becomes dirty saved when edited and remains when saved.
- A dirty saved state remains when edited and becomes clean saved when saved.
Hint: Use nested case checks.
Test: (next-editor-state :dirty-unsaved :save) returns :clean-saved, etc.
Number Parsing
Write a function parse-number that accepts a string parameter input which
shall be parsed as an integer to be returned.
Hint: Use Integer/parseInt to parse the number. Catch a possible
NumberFormatException, in which case a new exception is to be thrown using
ex-info.
Test: (parse-number "13") shall return 13, and (parse-number "foo") shall
throw an exception.