Scala – Day 1

I was looking forward to Chapter 5 of Seven Languages in Seven Weeks: Scala. I’ve heard quite a bit about it in the last few weeks at various user groups, and I’m hoping to get my hands on it at some point in my upcoming work with Atlassian, so this was a good time to dive in. As a personality, Scala is assigned Edward Scissorhands in the book: “awkward, and sometimes amazing”.

I tried at first to install it with Homebrew, which just failed with a 404, so I downloaded the package and installed it manually, which worked fine.

Day 1 was pretty straightforward – type a few things into the console and have a look at what you get back. This chapter delves into loops and ranges and compares Scala with both Java and Ruby, finishing up with some simple class definitions and traits. As with most of the chapters so far, it very quickly introduces a lot of ideas – not much detail, but enough to get me thinking.

In the self study for Day 1, the first questions are reasonably simple.

  1. Here’s a link to the Scala API
  2. There are lots of blog posts comparing java to scala, mostly just one aspect. I liked this write up based on a year of experience with Scala.
  3. A discussion of val versus var.

The next challenge was to write a game that will take a tic-tac-toe board (noughts and crosses for the Brits …) and determine a winner. The bonus part of the challenge would be to make it into a game that two people could play, so I attacked this part first.

I started off using Sublime Text 2, then decided to switch to IntelliJ with the Scala plugin. I like Sublime, but was hoping IntelliJ would give me better auto completion, refactoring tools and keyboard shortcuts. It seems to work OK – I had to point IntelliJ towards my Scala installation, and it is still popping up with some errors although it does compile and run just fine. Perhaps in Day 2 and 3 I’ll dig into those a bit more.

In writing the code for the game, I tripped up on a few things. I had Martin Odersky’s book Programming in Scala to refer to as well, which helped me solve most things really quickly.

Firstly that the chapter hadn’t covered how to return a specific type from a function. Scala doesn’t require the return keyword, but if you don’t specify a return value, it returns Unit().

Here’s a function without a return type:

def myMethod() {

}

And with:

def myMethod() : Int = {

}

In my next mistake, I tried to create an array and then add items to it – Arrays are mutable in Scala, but you can’t change the size of them. I didn’t even have a good reason for doing this, except that I thought it would make the code prettier, so I changed it to a List (immutable) instead. And it looked fine :)

I don’t know a lot about functional programming yet, so I did have a couple of classes in my solution. I wanted to make sure I had no mutable objects though.

When I finished my initial solution, I had three files – I ended up taking the lazy way of copying all the classes into the one file and running it from the terminal with scala tictactoe.scala. Here’s the initial attempt. I like that it doesn’t have any mutable objects, it’s simple, I don’t have to worry about blanks, and the simple map method to get the positions. I don’t much like the magic winning combinations in the Judge class, and I don’t like that if you don’t enter the moves in a valid format it will barf.

Next challenge: on to Day 2, and also trying to extend the tic-tac-toe game for the bonus challenge!

Frying my brain with Prolog

I recently picked up the Seven Languages in Seven Weeks book again, with the intention to start where I left off with Prolog, around the end of Day 1. As I was doing research for the exercises, I came across a great description that summed up exactly how I felt about this chapter:

“Today, Prolog broke my brain. The chapter started with recursion, lists, tuples, and pattern matching, all of which were tolerable if you’ve had prior exposure to functional programming. However, after that, we moved onto using unification as the primary construct for problem solving, and the gears in my head began to grind.”

At first, it seemed fairly easy to follow, very different to anything I’d done before, but that’s why I started with the Seven Languages book, to learn about new and different techniques in programming.

Reading through the day two section about lists and recursion, I started to find myself getting lost in the examples and it took a long time to understand what was going on. I couldn’t complete the day 2 exercises without a little help from the interwebz, although by the time I finished working through them, I did understand what I was going on. Trying to switch my brain from thinking about solving the problems in terms of rules rather than algorithms continued to bite me throughout the chapter though.

Some of the things I learned through Day 2 are pretty basic, but for a complete newbie to Prolog, they weren’t obvious.

In the factorial exercise, I realised that within a rule, I could add a line to validate the parameters – in this case that X > 0. Super obvious maybe, but Day 1 was all about matching rules and so this was new.

The next thing I learned was that you can have two versions of a rule with different conditions. I was already creating multiple versions of a rule to unify specific values such as 0 for factorial, but this was a different way to think about it.

As I worked through the sudoku and queens exercises, I still found myself wanting to do something like this:


Diags1 = [R1+C1, R2+C2, R3+C3, R4+C4, R5+C5, R6+C6, R7+C7, R8+C8],

… which just doesn’t work!

I did get there in the end with the queens solution, with a little help from the book to point me in the right direction for the diagonals.

In conclusion, I definitely learned a lot from this chapter, but struggled a lot as well! It was worth fighting through to the end though, as the concepts did start to make sense!