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:

  1. When the operation code is 0, add the two numbers
  2. When the operation code is 1, add the absolute values of the two numbers
  3. When the operation code is 2, add the absolute values of the two numbers and the absolute value of a variable named basis.
  4. When the operation code is between 10 and 20 (inclusive), add the absolute values of the two numbers and the operation code
  5. When the operation code is between 100 and 1000 (inclusive), multiply the absolute values of the two numbers and the operation code
  6. Otherwise, 0

The absolute value of a number x is mathematically defined by:

  1. When x is positive => x
  2. When x is negative => -x
  3. 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

  1. 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?.
  2. define a var named basis
  3. 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 <=

14.3   Solution

14.4   Second requirement

14.5   Second requirement

14.6   Third requirement

14.7   Fourth and fifth requirement

14.8   Last requirement

14.9   MISSION ACHIEVED