xxxxxxxxxx
case span (/=operator) unparsed of
-- If the element is not found we call the function recursivly searching for the next
-- operator in the register.
(_, []) -> eval rest unparsed
-- This is called if we do find a operator, we call the function from the register
-- and recursivly calculate the rest of the unparsed content before we send it to
-- the function.
(beforeOperator, AfterOperator) ->
function
(eval operatorRegister beforeOperator)
(eval operatorRegister $ drop 1 afterOperator)
xxxxxxxxxx
a list of strings = [String]
a integer = Int
a function that takes a integer and returns a string = Int -> String
a tuple of one integer and one string = (Int, String)
xxxxxxxxxx
type Operator = Double -> Double -> Double
type Entry = (String, Operator)
type Register = [Entry]
xxxxxxxxxx
operatorRegister :: Register -- The :: operator means 'is of type'
operatorRegister = [
("+", (+)),
("*", (*))
]
xxxxxxxxxx
calculate :: String -> Double
-- operatorRegister is the register we defined before.
calculate = eval operatorRegister . words
eval :: Register -> [String] -> Double
eval = undefined
xxxxxxxxxx
eval :: Register -> [String] -> Double
eval _ [number] = read number
eval ((operator, function):rest) unparsed =
case span (/=operator) unparsed of
(_, []) -> eval rest unparsed
(beforeOperator, afterOperator) ->
function
(eval operatorRegister beforeOperator)
(eval operatorRegister $ drop 1 afterOperator)