I am trying to implement famous Verbal arithmetics problem (SEND + MORE = MONEY) in Haskell. For those who don't know about verbal arithmetics- https://en.wikipedia.org/wiki/Verbal_arithmetic.
This is my sample program, however for some reason it does not work, it returns an empty list. Could anyone point out the flaw in the code and give some suggestions? I am stuck, thank you!
expand' a b c d e = e + d*10 + c*100 + b*1000 + a*10000
expand'' a b c d = d + c*10 + b*100 + a*1000
digits = [0,1..9] -- all the digits
answer = head [ list | list@[s,e,n,d,m,o,r,y] <- perm digits,
expand'' s e n d + expand'' m o r e == expand' m o n e y]
--unique permutation
perm :: [t] -> [[t]]
perm [] = [[]]
perm (x:xs) = [(y:zs) | (y,ys) <- del (x:xs), zs <- perm ys]
--deleting duplicates
del :: [t] -> [(t,[t])]
del [] = []
del (x:xs) = ((x,xs) : [ (y,(x:ys)) | (y,ys) <- del xs ])
main :: IO()
main = do
print answer
The solution was inspired by: http://www.dcs.gla.ac.uk/mail-www/haskell/msg01929.html