リストモナドで解く
(追記: http://haskell.g.hatena.ne.jp/nobsun/20061019/alphametic のほうがとてもスマート。)
ten = [0..9] getNum :: [Int] -> [(Int,[Int])] getNum ns = do{n <- ns; return (n, filter (n/=) ns)} sendmory l0 = do (s,l1) <- getNum l0 if s==0 then [] else return () (m,l2) <- getNum l1 if m==0 then [] else return () (e,l3) <- getNum l2 (n,l4) <- getNum l3 (d,l5) <- getNum l4 (o,l6) <- getNum l5 (r,l7) <- getNum l6 (y,l8) <- getNum l7 if s*1000+e*100+n*10+d+m*1000+o*100+r*10+e==m*10000+o*1000+n*100+e*10+y then return (s,e,n,d,m,o,r,y) else [] instance (Show s, Show e, Show n, Show d, Show m, Show o, Show r, Show y)=> Show (s,e,n,d,m,o,r,y) where show (s,e,n,d,m,o,r,y) = "("++show s++","++show e++","++show n++","++show d++","++show m++","++show o++","++show r++","++show y++")" main = print (sendmory ten)
コンパイルすればまずまず速い。
ここで、Stateモナドなパターンが現れているので: