haskell]地下鉄の求人広告3

難しく考え過ぎていたことに気づいた。

And the more I see - the more I know
The more I know - the less I understand. 
Paul Weller - The Changing Man

(追記)
なんか勘違いしてた。穴があったら入りたい。
(追追記)
修正。

module Main where

import Debug.Trace

p1 = ["one","two","three","four","five","six","seven","eight","nine"]
p2 = ["ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"]
p3 = ["twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]

line 1 = p1
line 2 = p2 ++ [x ++ y | x <- p3, y <- ("":p1)]
line 3 = line' "hundred"  (line 1) (("":(line 1)) ++ (line 2))
line n | n > 3 && n < 7 = line' "thousand" (line (n - 3)) (("":(line 1)) ++ (line 2) ++ (line 3))
       | otherwise      = line' "million"  (line (n - 6)) (("":(line 1)) ++ (line 2) ++ (line 3) ++ (line 4) ++ (line 5) ++ (line 6))

line' s p q = [x ++ y | x <- map (++s) p, y <- q]

recur :: Int -> Integer -> [String] -> Char
recur n idx [] = trace (show n ++ ":" ++ show idx) $ recur (n + 1) idx (line (n + 1))
recur n idx (x:xs) = let i = idx - (toInteger (length x))
                     in if i <= 0 then x !! (fromInteger idx) else recur n i xs

five_one_billion :: Integer
five_one_billion = 5100000000

main = print $ recur 0 five_one_billion []