5. The Type Zoo | A Crash Course In Algebraic Types | |
Previous | Up | |
Arithmetic expression interpreter
Specification
An arithmetic expression is one of the following:
Evaluating an arithmetic expression requires the following steps:
Implementation
First, it's possible to produce an almost line-for-line copy of the
specification as a type:
The definition of the evaluation function is similarly trivial, and matches
the textual specification:
The run function is an example of an
equational definition. The definition
is the same as using a case expression
in the body of the function, but more syntactically compact.
It's possible to evaluate expressions from the ghci
interpreter for some assurance that the function really is correct:
The code has a large number of advantages over the original Java code:
Previous | Up | |
5. The Type Zoo | A Crash Course In Algebraic Types | |