Learn You Scala for Great Good!

I came to Scala from Java. So when I wanted to learn Scalaz, I didn’t know what to start with. I decided that the best way would be to start with functional programming. It’s strange but the best manual was Learn You a Haskell for Great Good!

Yes, I chose my way: I started learning Haskell in order to know Scala better!

For better understanding I started to “translate” Haskell examples into Scala. Now I have decided to publish these examples.

We’re going to make a simple function that berates you differently depending on your BMI (body mass index). Your BMI equals your weight divided by your height squared. If your BMI is less than 18.5, you’re considered underweight. If it’s anywhere from 18.5 to 25 then you’re considered normal. 25 to 30 is overweight and more than 30 is obese. So here’s the function (we won’t be calculating it right now, this function just gets a BMI and tells you off)

Guards are indicated by pipes that follow a function’s name and its parameters. Usually, they’re indented a bit to the right and lined up. A guard is basically a boolean expression. If it evaluates to True, then the corresponding function body is used. If it evaluates to False, checking drops through to the next guard and so on. If we call this function with 24.3, it will first check if that’s smaller than or equal to 18.5. Because it isn’t, it falls through to the next guard. The check is carried out with the second guard and because 24.3 is less than 25.0, the second string is returned.