The binding is only available if th expression is truthy.
If returning nil in the alternative case is sufficient, use when-let:
(def gifts {:alice"purse":bob"hat"})
(defn handout [customer]
(when-let [gift (get gifts customer)]
(str "You get a " gift ".")))
(handout:alice) ; "You get a purse."(handout:bob) ; "You get a hat."(handout:chaim) ; nil
Exercises
Temporary Values
Rewrite the solution for exercise Map Update Function from the last chapter
with let bindings so that the code becomes more readable.
Hint: Bind the result of each update operation to a new variable.
Write a function format-duration that, given a duration in seconds, returns a
string of the following format: XhYmZs, with X indicating hours, Y
indicating minutes, and Z indicating seconds.
Hint: Use quot and mod for the calculation and let for bindings. Omit any
indications equal to 0 (e.g. 5m13s or 1h30m instead of 0h5m13s or
1h30m0s, respectively).
Write a function print-angle that expects a decimal angle in degrees, e.g.
32.8451 and converts the decimal portion to arcminutes and arcseconds (i.e.
1/60 and 1/36000 parts of a whole degree). Print the angle formatted as
x°y'z".