java progran

profileabcstol1
proassign.pdf

PROGRAMMING ASSIGNMENT 2:

FUNCTIONAL PROGRAMMING INFIX EXP RES SION C AL C UL ATOR

P R E L I M I N A R Y N O TE S

You have your choice of programming languages for this assignment:

 Scheme (or Clojure)

 Ocaml (or F#)

 Java 8 (utilizing the “Functional Programming” features of the language

 Scala

R U L E S

(NOTE: Since a choice of languages is given, the “rules” have to remain flexible, hence the use of where applicable

in several of the rules below.)

 Must use “High Order Functions” where applicable.

 The only permitted “looping constructs” are:

o Recursion

o Streams, Maps, Folds

o In some pure functional languages, these would be your only options

 Must use “strong” and proper datatypes where applicable

o For example each kind of operator would be its own type.

 (E.g. Addition and Subtraction would both be instances of an “Additive” type, but

Multiplication and Division would be instances of a “Multiplicative type”)

 In other words, each “precedence level” should be represented by its own type.

 Should use floating-point input and evaluation.

P R O G R A M D E S C R I P TI O N

1. Write a program which accepts a space-delimited set of symbols representing an infix arithmetic expression

as described below.

a. The user should be prompted for the input (to be entered via keyboard)

2. Output:

a. The numeric evaluation of the expression

b. A string representing the arithmetic expression as an S-Expression

i. Hint: Easily generated from a simple pre-order tree traversal

ii. Note that any parentheses in the original input should not appear in the output (ie.

Duplicating parentheses)

E X A M P L E :

Enter infix expression: 4 ! / 2 + 9 * 5

Numeric Result: 57

S-Expression: ( + ( / ( ! (4) ) (2) ) (* (9) (5) ) )

F U N C TI O N S A N D P R E C E D E N C E :

Precedence Associativity Operator Note

1 LR + Add

1 LR - Subtract

2 LR * Multiply

2 LR / Divide

3 LR > Greatest Common Factor: 15 > 18 would resolve to 3

3 LR < Least Common Multiple: 15 < 18 resolves to 90

4 RL ^ Exponent: 4^3^2 resolves to 262144

5 n/a ! Factorial: a Unary Operator

6 n/a ( Open Parentheses

6 n/a ) Close Parentheses