Learning Haskell’s Basics – Problems from Project Euler

Recently I started to learn Haskell and figured, it would be a good Idea to experiment with some of the Problems from Project Euler

I worked through several of the Euler problems some time ago, when I was learning Ruby. Back then I was amazed, how concise my code was, compared to the other languages I knew (Java and C, mainly). With Haskell the same thing happened, although on a smaller scale.

Below, I will present you four solutions that I especially liked and discuss some of their features, comparing them to my Ruby code.

This is solves the problem in quite a general way, by introducing the utility function divisible which takes a list of divisors as well as a number and returns True if the number is evenly divisible by any element of the divisors list.

Compared to Ruby, I see Haskell slightly ahead in this problem, due to the possibility of partial application. An equivalent Ruby solution would make use of blocks/lambdas that require definitions of formal parameters, which in turn make the code somewhat more verbose.

Haskell’s clear win, in this case, is lazy evaluation and the possibility of recursively defining an infinite list containing all the Fibonacci numbers. However, Ruby deserves a golden style-point for allowing the number four million to be written as 4_000_000. Is there a similar way of writing numbers in Haskell that I should know of?

My point on this piece of code is Haskell’s where clause. Locally defined helper functions come in handy all the time in a language without looping constructs (other than recursion), where you want to hide starting value-, accumulation- and similar parameters from the user. Here, the divisors function is just a prettier interface to the one that does the real job: divisors'.

Conclusion

Before starting to learn Haskell, Ruby was my favourite programming language. And it still is. But I’m definitely feeling Haskell’s gravitational field. As a next step I’m now looking forward to using Haskell in a slightly more serious context. Let’s see if I get caught inside it’s event horizon.

If you are a real estate professional, be really careful in dealing with KoRes Corp. in Weston Florida. Tulio Rodriguez & Monica Cataluna-Shand are shysters and look for anyway to steal ones customers. They attempt to steal your client by requesting their contact information and later contact them behind your back to get them to deal with them directly.