# Archive for September, 2010

## Dream

1

What a horrible horrible dream!
I was a terrorist on a killing spree, in it.

How terrible my conscience even let me play a role like that;
Knowing fully well that I’d much rather kill people for fun than for having different opinions than mine. 😀

Atleast, my roomie played some loud music early and jolted me awake.
This one time, I’ll forgive him. 🙂

## Project Euler Problem 22

0

Whilst trying Project Euler problems, I often write a prototype in Python before trying out an implementation in Haskell.
This problem however was screaming for “Functional Programming”.

In fact this time around, I started with a Haskell implementation and went on to translate to Python just for kicks. 🙂

Suffice to say, I am very pleased with myself. 😀

import Char
import List

names = [<List of names from problem specification>]

{- For each string s in list, Calculate sum of the characters’ alpha positions -}
sumChars :: [String] -> [Int]
sumChars [] = []
sumChars (x:xs) =
(foldl (\acc ltr -> acc + (ord(ltr)-ord(‘A’) + 1)) 0 x) : sumChars xs

main = do
let sortedNames = sort(names)
let zippedSumAndIndex = zip [1 .. length(sortedNames)] (sumChars sortedNames)
let total = foldl (\x y -> x + fst(y) * snd(y)) 0 zippedSumAndIndex
print total

## Tears

0

Her tears were precious indeed.
They almost cost me my life when she cried
Telling me why she can never be mine.

Go to Top