devNoo

musings of a Software Developer

11 November 2012
by Job
0 comments

Seven languages in seven weeks: My solutions for haskell day 1

This are my solutions for the first Haskell chapter of the book seven languages in seven weeks.

I never touch Haskell, and only a bit of functional programming (mainly from this book), so I’m looking forward to have a look at Haskell, seen it is a functional languages that a lot of people refer to.

The first chapter shows the basics of the languages, so how to define functions, how lists work, how list comprehension works, tail recursion etc.

Exercise 1

The first exercise is quite a good start to look in to the possibilities of the language. The assignment is to create as many as possible different ways to implement an all even function.

Here is my go at it:
Continue Reading →

6 November 2012
by Job
0 comments

Seven languages in seven weeks day : My solutions for clojure day 3

Day 3 of clojure was al about concurrency. It introduces the software transactional memory, references, agents, futures and atoms.

The first exercise

The first exercise is to create a vector of accounts in memory, and create a debit and credit function to change the balance of an account within the vector.

I just implemented it in a very basic way, using a reference and accessing it with the dosync function.

Nothing else to say about it, except it is really a small about off code you need to have a program supporting concurrency.

Exercise 2

The next exercise was a lot more challenging. It is the sleeping barber problem as described on this link.

Here is my solution.

So I basically used two atoms to represent the state that is to be manipulated by concurrent threads. And in the functions I use swap to alter the data with the atoms.

Conclusion

All with all I really grew to like clojure. The first few times someone showed it to me, I really didn’t understand a thing, not being used to the lisp like syntax, but when you get accustomed to it, it gets a lot easier to read, and in the end I really found the code quite beautiful.
If I compare it to scala, I find clojure a neater, more powerful language(based on this 3 chapters, so basically nothing ;-) ) and would rather invest some time in clojure.

Ok next chapter will be haskell.

25 September 2012
by Job
0 comments

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

Day 2 in clojure is about macro’s, sequences, protocols and records.

The first exercise deals with macro’s, the assignment is to extend the unless example from the book with an else body.
It is a fairly straight forward exercise, and there is not much to say about my implementation.


Continue Reading →

8 August 2012
by Job
0 comments

Seven languages in seven weeks: solutions for clojure day 1

Clojure was a language I looked forward to learn about, mostly because I didn’t understand any code that was showed to me. The first day of clojure takes it slowly, but helps you get a grip on the lisp like jvm language. Installing clojure on windows was just extracting a zip file. In IntelliJ I installed the La clojure plugin. This seams to be a great plugin, although I only used its console feature, (is the autocomplete coming from clojure repl, or is it La console?).

Continue Reading →

30 July 2012
by Job
0 comments

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

Day 3 of erlang was all about concurrency. This is the place where erlang really shines.

As a first exercise you have to look into the OTP documentation. OTP is a framework/library that makes concurrency in erlang even simpler. There are some base templates that offer you all kinds of client/server functionality. Here is a great description of the design principles of OTP: http://www.erlang.org/doc/design_principles/des_princ.html.

 

Continue Reading →

19 July 2012
by Job
0 comments

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

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.
Continue Reading →

6 March 2012
by Job
0 comments

Seven languages in seven weeks , my solutions for Erlang day 1

In this article I will present my solutions for the first chapter on erlang in the book Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers).

Erlang is new for me, but it is based on prolog which is also included in the seven languages. The first chapter just introduces erlang, talks a bit about it strongpoints and weaknesses and introduce you to the syntax and basic functions.

The first exercise is to write a word counter that uses recursion to count the words in a string. For now I assumed that words are only separated by a space, character 32. Here is my solution:

I needed quite some time and googling to come up with the solutions, mainly because I’m not used to the prolog/erlang style of programming.

The second exercise was a lot easier, you had to write a program that counts to ten, using recursion.

This was quite simpel, I just used pattern matching to stop at 10, off course the order of the statements becomes important here if you switch the last lines it will probably count to infinity.

Last exercise was to write a function that uses pattern matching to printout a succes or an error message.

1 March 2012
by Job
0 comments

Seven languages in seven weeks, my solutions for scala day 3

In day 3 of the Scala chapter of the book seven languages in seven weeks we dive deeper in functional programming and parallelization with scala. I run into some problems doing the exercises, one of them being that the Source.fromUrl method, after putting in the encoding, there were still some problems when following links. So I had to put a try catch around it. Normally you might need a bit more robust solution, but for now this will do ;-)

The first exercise was kind of simple, you had to adjust a given “sizer” script to include a link count. There was already a foldLeft over the links so I only had to add an int var and a increment of this counter, and off course return the result.

At work a colleague once talked about the idea of tupels, and I wanted to introduce this also in my Java code. I was taken aback from the idea back then since you would only need such things if your methods do more than one thing or you are to lazy to introduce an object for the results that belong together. But because he learned about tuples in Scala (and I was lazy myself) I wanted to give it a try. So I adjusted the existing method that returned the size, to also return the link count.
Continue Reading →

20 February 2012
by Job
0 comments

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

Finally I have time and energy to continue my voyage trough the book Seven languages in seven weeks. The chapter focuses on collections and the functional programming that you can do on them.
Well because it is some time I go I read the chapter and did the exercises I will just include the solution without further ado.

Use fold left to compute the total size of a list of strings

For a good introduction to fold left and fold right look at this thread on stackoverflow.

Censoring text
The second exercise consists of writing a censor trait than will offer functionality to censor the words Shoot and Darn in a given text.

Censor dictionary
The next exercise is the same as above, but now the text had to come from a file.

.

Ok this was all for scala day2, please leave comments on the code.

12 January 2012
by Job
5 Comments

Seven languages in seven weeks: My solutions for scala day 1

This post will present my solutions for day 1 of the Scala chapter in the book Seven languages in seven weeks:

I really looked forward to the Scala chapter, also because Scala has generated quite some buzz lately. My first encounter with Scala was some 3 years ago on a NL-Jug conference. Since then I’m not a fan of scala, but I doubt that I gave it a fair chance. My dislike especially came from people’s reasoning that in Scala you could do things in less code and so this must be better than Java.

Of course Java brings some verbosity, but it also brings a syntax that is understandable for almost any programmer (so also programmers that never learned Java), and I clearly missed this in a lot of the examples the scala people came up with. Fibonacci in one line of code may seem cool for the writer, but most certainly is not cool for the readers, and seen that code is read at least 10 times as much as written, the argument that you have to write less code should never prevail the argument of readability.

But I might have been blinded by my dislike of this reasoning and programming style, and didn’t give Scala a fair chance. So I will try to give Scala a chance in the coming 3 blog entries, trying to keep a Beginner’s mind as mentioned by Patrick Kua in this presentation. BTW if you like the presentation you will probably also like the this book: Pragmatic Thinking and Learning: Refactor Your Wetware (Pragmatic Programmers).
Continue Reading →