Basic Scheme Math Composition of Functions
Description:
Scheme Reference. This is a more difficult version of the Basic Scheme Math Interpreter.
Your challenge: create a function that interprets Scheme-like math commands and returns the value resulting from the operation. We will only be working with the four main math operators (+ - * /)
but the functions are nested this time.
Syntax consists of an open parenthesis, a math operator, a space, space-separated arguments, and a close parenthesis. Example: (+ 2 4)
. All inputs will be valid. Keep in mind that we're composing functions now, so the inputs will be a little more complex, such as (+ (* 4 2) (/ 4 2))
.
That example, (+ (* 4 2) (/ 4 2))
, can be rewritten as (4 x 2) + (4 / 2)
and is calculated as follows:
1. 4 x 2 = 8 --> (+ 8 (/ 4 2))
2. 4 / 2 = 2 --> (+ 8 2)
3. 8 + 2 = 10 --> 10
If no arguments are provided, return the following (You won't get '(/)'
):
'(+)' // 0
'(-)' // 0
'(*)' // 1
if one argument is provided, return the following:
'(+ n)' // n Returns the sum of the arguments: 0 + n...
'(- n)' // -n Returns the additive inverse of n: 0 - n
'(- -n)' // n Returns the additive inverse of n: 0 - (-n)
'(* n)' // n Returns the product of the arguments 1 * n...
'(/ n)' // 1/n Returns the multiplicative inverse of n: 1 / n
Examples of valid commands (do not parse -0 to 0...):
'(+ 2 4)' // 6
'(+ (* 4 2) (/ 4 2))' // 10
'(+ 4 (- 4 (+ 2 2)) -4)' // 0
'(- (+ 2 4) (+ 2 4))' // 0
'(* (+ 2 4) (- 2 4) (/ 4 2))' // -24
'(/ (+ 4 (* 2 4)) 4)' // 3
'(+ 0 (/ 1 (* 14 (* 14 14 14 14) (* 14 14 14 14))))' // 4.8400258247050876e-11
'(/ 1 (* 0 -1))' // -Infinity
'(* 1 -0)' // -0
'(* 0 -1)' // -0
'(+ 3 4 (+ 10 5) (+ (+ 17 7) (+ 7 2)))' // 55
'(- 3 4 (- 10 5) (- (- 17 7) (- 7 2)))' // -11
'(* 3 4 (* 10 5) (* (* 17 7) (* 7 2)))' // 999600
'(/ 3 4 (/ 10 5) (/ (/ 17 7) (/ 7 2)))' // 0.5404411764705882
Similar Kata:
Stats:
Created | Dec 26, 2018 |
Published | Dec 26, 2018 |
Warriors Trained | 246 |
Total Skips | 4 |
Total Code Submissions | 839 |
Total Times Completed | 54 |
JavaScript Completions | 52 |
Haskell Completions | 4 |
Total Stars | 11 |
% of votes with a positive feedback rating | 77% of 24 |
Total "Very Satisfied" Votes | 17 |
Total "Somewhat Satisfied" Votes | 3 |
Total "Not Satisfied" Votes | 4 |
Total Rank Assessments | 5 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 8 kyu |