Functional Scala_ a little expression language with algebraic datatypes and pattern matching _ brain - Functional Scala a little expression language

# Functional Scala_ a little expression language with algebraic datatypes and pattern matching _ brain

• Homework Help
• 11

This preview shows page 1 - 3 out of 11 pages.

14/04/2017 Functional Scala: a little expression language with algebraic datatypes and pattern matching | brain driven development 1/11 Functional Scala: a little expression language with algebraic datatypes and pattern matching March 6, 2011 — Mario Gleichmann Welcome to another episode of Functional Scala! In this episode we’ll going to recap and employ everything we’ve learned so far about algebraic datatypes and pa±ern matching for implementing a more extensive example. We’ll put all those li±le pieces together which we’ve discovered so far and see how they work in cooperation for building a li±le language which allows us to write, pre±y print, step‑wise reduce and evaluate arbitrary arithmetic expressions (considering addition, subtraction and multiplication on integer values). And as the cherry on top, we’ll discover some new ﬂavour of algebraic datatypes and pa±ern matching. It may be a good start to first give some thoughts on how we wanna represent algebraic expressions. Just consider a language of expressions build up from basic values (integer literals) and some operations on them (e.g. addition or multiplication of some integer values). For example, the following are all arithmetic expressions: 17 17 + 23 109 * ( 8 + 15 ) An expression in its simplest form might be an integer literal. That’s a legal expression for sure! And how could we represent such an entity within our li±le language? What about an algebraic datatype Expression ? Right, mate! Good idea! Let’s write it down: Ok, so far we have a simple and neat product type, since we could express a whole bunch of different integer literals, just using that single value constructor: Well, if you think that’s boring, you’re again right! So far, our Expressions are a bit too simple, right? 1 2 sealed abstract case class Expression case class Literal( x : Int ) extends Expression 1 2 3 4 val expr 1 = Literal( 1 ) val expr 2 = Literal( 17 ) ... val exprN = Literal( 532 ) brain driven development
14/04/2017 Functional Scala: a little expression language with algebraic datatypes and pattern matching | brain driven development 2/11 Well, if you think that’s boring, you’re again right! So far, our Expressions are a bit too simple, right? How do you feel about adding some operations which will take some integer literals and act on them? The addition of two integer literals surely pose an Expression in itself. So let’s add it as another value constructor to our datatype, extending our expression language: Aaahh, now things start to become more interesting. Our new value constructor also features some components, that is two integer literals which act as the operands for our operation Add .

#### You've reached the end of your free preview.

Want to read all 11 pages?

• Winter '17
• Smith Jones

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern