6 kyu

Basic Scheme Math Composition of Functions

52 of 54user2420289

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
Mathematics
Algorithms

Stats:

CreatedDec 26, 2018
PublishedDec 26, 2018
Warriors Trained246
Total Skips4
Total Code Submissions839
Total Times Completed54
JavaScript Completions52
Haskell Completions4
Total Stars11
% of votes with a positive feedback rating77% of 24
Total "Very Satisfied" Votes17
Total "Somewhat Satisfied" Votes3
Total "Not Satisfied" Votes4
Total Rank Assessments5
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • user2420289 Avatar
  • JohanWiltink Avatar
  • SQSCWQ Avatar
Ad