May 2018
Beginner to intermediate
290 pages
6h 43m
English
Technically, no matter how complicated the decision you need to make, the plain old if is all you ever need. Choosing between three alternatives? Just nest a couple of ifs. Say, for example, our shipping charges were free to preferred customers and otherwise $5 for orders under $50, $10 for orders between $50 and $100, and 10% of the purchase price for bigger orders, we could write something like this:
| | (defn shipping-charge [preferred-customer order-amount] |
| | (if preferred-customer |
| | 0.0 |
| | (if (< order-amount 50.0) |
| | 5.0 |
| | (if (< order-amount 100.0) |
| | 10.0 |
| | (* 0.1 order-amount))))) |
Have another alternative? Then just nest another if—and at some point drive yourself crazy. While your CPU may be fine ...
Read now
Unlock full access