Sunday, April 1, 2012

Code snippet of the day: Haskell for the dumb idiot lazy programmers

The euler project problem reads as such, "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000."
Here is one of many implementations in Haskell.  I used a verbose recursive approach, iterate up to 1000 and then build a list with the items of interest.  In this case, 'multiples of 3 or 5'.  The first implementation contains a logging utility for writing a string at each iteration.

Figure 1: Euler Problem1 in Haskell

Here is the second source snippet, I just wanted to provide something more practical, a log parsing example that you can run against your web log files.

Figure 2: Applied Haskell, simply read each line of a file, find a term and output the results to another file.

Source:

No comments: