所以我正在尝试使用频率分析来破译代码。
import Data.Char
import Data.List
import Data.Function
import qualified Data.Map as DMap
codedMsg = "V'Z GELVAT GB GRNPU GUR PNIRZRA GB CYNL FPENOOYR. VG'F HCUVYY JBEX. GUR BAYL JBEQ GURL XABJ VF 'HAU', NAQ GURL QBA'G XABJ UBJ GB FCRYY VG."
mostFreqLtr = ["E", "T", "A", "O", "I", "N", "S", "H", "R", "D", "L", "C", "U", "M", "W", "F", "G", "Y", "P", "B", "V", "K", "X", "J", "Q", "Z"]
--weed out non alphabetical characters from the list
alphaSort lst
| null lst = []
| isAlpha (head lst) = (head lst) : alphaSort (tail lst)
| otherwise = alphaSort (tail lst)
--sort the list by characters
msgSort [] = []
msgSort lst = sortBy (compare `on` ord) lst
--group each character into it's own list
grp [] = []
grp lst = group lst
--sort the list into most frequent character first
lSort [] = []
lSort lst = reverse (sortBy (compare `on` length) lst)
--change the list into one instance of each character
oneChar [] = []
oneChar lst = take 1 (head lst) : oneChar (tail lst)
--Pairing letters and creating a map of tuples containing frequency related characters
msg = zip (oneChar $ lSort $ grp $ msgSort $ alphaSort $ map toUpper $ codedMsg) mostFreqLtr
msg2 = DMap.fromList msg
--replace coded list with analyzed list
replaceChars lst
| null lst = []
| isAlpha (head lst) = DMap.lookup (head lst) msg2 : replaceChars (tail lst)
| otherwise = (head lst) : replaceChars (tail lst)
result = replaceChars codedMsg
我不断收到此错误:
Couldn't match expected type `Char' with actual type `[Char]'
Expected type: DMap.Map Char a0
Actual type: DMap.Map [Char] [Char]
In the second argument of `DMap.lookup', namely `msg2'
In the first argument of `(:)', namely
`DMap.lookup (head lst) msg2'