# Archive for September, 2010

## Project Euler Problem 22

0Whilst 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