Tag Archives: Haskell

another simple list comprehension

— Naive (and slow: 1.4 seconds) solution to:
http://bartoszmilewski.com/2015/05/14/using-monads-in-c-to-solve-constraints-2-the-state-monad/

-- Sendmoremoney.hs
import Data.List(nub)
import Text.Printf(printf)
 
printer :: Int -> Int -> Int -> Int
    -> Int -> Int -> Int -> Int
    -> Int -> Int -> Int -> Int -> Int
    -> String

printer = printf "%d%d%d%d + %d%d%d%d = %d%d%d%d%d"

main = mapM_ putStrLn $
  [ printer s e n d m o r e m o n e y |
    d <- [0..9] ,
    e <- [0..9] ,
    m <- [1..9] ,
    n <- [0..9] ,
    o <- [0..9] ,
    r <- [0..9] ,
    s <- [1..9] ,
    y <- [0..9] ,
    s*1000+e*100+n*10+d
    + m*1000+o*100+r*10+e
    == m*10000 + o*1000+n*100+e*10+y
    , ( length $ nub [d,e,m,n,o,r,s,y ] ) == 8
  ]
 

— PS C:\dev\hask> Measure-Command { .\sendmoremoney.exe }  | select TotalSeconds | Format-List

— TotalSeconds : 1.3789969