March 2019
Intermediate to advanced
208 pages
5h 11m
English
Let’s add a bit of complexity to our price calculator: discounts. If you order fewer than ten items, you get a 5% discount. Otherwise, you get a 10% discount. We want the program to:
Calculate the total price.
Calculate the appropriate discount rate.
Calculate the cost with and without discount.
Print both, properly labeled.
Step 2 requires us to use an if/else expression:
| | let discount = |
| | if (qty < 10) { |
| | 0.05; |
| | } else { |
| | 0.10; |
| | }; |
In ReasonML, the if keyword introduces an expression, not a statement. This crucial difference means you can bind the result of an if expression to a variable. It also means you must provide an else clause, and the results of both ...
Read now
Unlock full access