devNoo

musings of a Software Developer

Seven languages in seven weeks : My solutions for erlang day 2

| 0 comments

I finally continue my series of blog post presenting my solutions for the exercises of the book Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers).

So here are my solutions for erlang day 2.

Exercise 1
The first exercise is to create a function that gets the description of a programming languages when presented with a list containing languages and descriptions. I used a list comprehension with some simple pattern matching to get this done. It is quite simple straight forward code, but a bit too simple example in my opinion, this will be easy and short in java and other more verbose languages as well.

Exercise 2
Given a shopping list that looks like [{item, quantity, price}] return a list containing the items and the total price per item, using a list comprehension.

This is a better example of the power of pattern matching, especially in combination with lists.

Exercise 3
This exercise is to solve the state of a tictactoe board, presented as a list of 9 elements. So for example
tictactoe:state([x,x,x,o,o," ", " ", " ", " "]) should return x as winner. I had some difficulties on how to handle this, and when I was googling for other solutions I found this solution : http://mrjaba.posterous.com/7-languages-in-7-weeks-erlang-day-2-exercises. I didn’t think of using pattern matching as a solution for this problem but it is quite powerful. I refactored it a bit so there is less repetition, but the idea is still the same.

conclusion for day 2
I found the list comprehensions and the pattern matching in erlang really powerful. But I’m hoping that day 3 will introduce some more features that really make erlang stand out.
Hopefully I will be able to post the solutions to day 3 quicker than this one.

Leave a Reply

Required fields are marked *.

*