Lesson 14. Capstone project
14.1 Your mission
Your mission to write in the Clojure REPL a function named combine-numbers that receives an operation code and two numbers and return a result based on the operation code:
- When the operation code is 0, add the two numbers
- When the operation code is 1, add the absolute values of the two numbers
- When the operation code is 2, add the absolute values of the two numbers and the absolute value of a variable named basis.
- When the operation code is between 10 and 20 (inclusive), add the absolute values of the two numbers and the operation code
- When the operation code is between 100 and 1000 (inclusive), multiply the absolute values of the two numbers and the operation code
- Otherwise, 0
The absolute value of a number x is mathematically defined by:
- When x is positive => x
- When x is negative => -x
- When x is zero => 0
Note that requirement #3 makes combine-numbers an impure function as it depends on an external factor: the value of the variable basis.
14.2 Guidance
- Write a 1-arity function named abs that receives an argument named x and calculates the absolute value of x. The body of abs is made of an if expression that combines pos?, neg? and zero?.
- define a var named basis
- Write a 3-arity combine-numbers that receives operation-code, x and y. The body of combine-numbers is made of a cond expression that combines abs, basis, =, + and <=