【发布时间】:2015-01-23 02:28:19
【问题描述】:
我正在尝试解决 Project Euler 问题 59,我必须对具有三个字符密钥(全小写)的消息进行异或解密。在 ASCII 中,这意味着所有键的集合是
let keys = [[a, b, c] | a <- [97..122], b <- [97..122], c <- [97..122]]
以下函数将一起启发式地解密它:
decrypt :: [Int] -> [Int] -> [Char]
decrypt msg xs = map chr $ zipWith xor msg $ (concat . repeat) xs
try :: [[Int]] -> [Int] -> [Char]
try kys msg = head $ filter (isInfixOf "the") $ map (decrypt msg) kys
基本上我一直在尝试使用密钥,直到其中一个解密消息以在其中包含“the”(我知道该消息具有常见的英文单词)。但是当我绑定 keys 和 message 并运行 try keys message 我得到
Couldn't match expected type `Int' with actual type `Integer'
Expected type: [[Int]]
Actual type: [[Integer]]
In the first argument of `try', namely `keys'
In the expression: try keys message
现在,即使我说let keys = [map fromIntegral [a, b, c] | a <- [97..122], b <- 97..122],, c <- [97..122]],它仍然说它具有Integer 类型而不是Int,当我尝试let keys = map (map fromIntegral) keys 时,以及当我使用fromInteger 时也是如此。怎么回事?
【问题讨论】: