2

所以我正在尝试使用频率分析来破译代码。

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'
4

2 回答 2

3

在所有顶级函数上编写类型签名。然后你会发现

oneChar :: [[a]] -> [[a]]

同时,从使用来看,我猜你打算

oneChar :: [[Char]] -> [Char]

而不是take 1,您应该使用head,或者您应该concat编辑结果以获得Chars 列表。

照原样,您构造的映射msg2具有[Char]键,但您尝试使用它,就好像它具有Chars 作为键一样。

于 2012-06-21T18:28:29.437 回答
3

嗯 - 我坐下来想了想你的代码

  • 请使用类型签名它对您的代码有很大帮助-编译器也可以优化

  • 起一些更有意义的名字

    • mostFreqLtr-> freqTable_EN(这表明您正在破译英文文本)
    • alphaSort-> filterAlpha(这是一种误导,因为您过滤的是非字母元素而不是排序任何东西
    • msgSort- > sort(因为它是一样的,我认为)
  • 使用模式匹配而不是headand tail
    ie lst ... head lst ... tail lst ->lst@(c:cs)
    • 然后 lst 可以作为它自己引用,并且c是它的头部和cs尾部(单个元素通常被称为单个字母,列表作为它们的准复数并附加s

代码:

import Prelude hiding (lookup)
import Data.Char ( isAlpha
                 , toUpper)
import Data.List ( group
                 , sort
                 , sortBy)
import Data.Function (on)
import Data.Map ( fromList
                , lookup
                , Map)
import Data.Maybe (mapMaybe)

只导入必要的代码位

codedMsg :: String
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."

freqTable_EN :: [Char]
freqTable_EN = ['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']

不要使用太长的行——它会降低代码的可读性,姓氏freqTable_EN很不寻常,但在这种情况下,我可以随意偏离标准,因为它更具可读性。我还使用[Char]而不是String(等效的)来更清楚地表明它是一个字母表。

-- weed out non alphabetical characters from the list
filterAlpha :: String -> String
filterAlpha = filter isAlpha

-- sort a list by length
sortByLength :: [[a]] -> [[a]]
sortByLength = sortBy (compare `on` length)

-- sort the list into most frequent character first
sortByFreq :: [[a]] -> [[a]]
sortByFreq = reverse . sortByLength

好的函数名不需要这样的注释

-- change the list into one instance of each character
reduceGroups :: [[a]] -> [a]
reduceGroups lst = map head lst

你也可以让lst编译器足够聪明地从类型签名中获取所有信息,所以最后一行也可以是reduceGroups = map head

-- Pairing coded message with frequency table
pairs :: [(Char, Char)]
pairs = nonAlphaPairs ++ zip freqSortedMsg freqTable_EN
  where cleanedMsg    = (filterAlpha . map toUpper) codedMsg
        freqSortedMsg = (reduceGroups . sortByFreq . group . sort) cleanedMsg
        nonAlphaPairs = map (\x ->(x,x)) $ filter (not . isAlpha) codedMsg

(\x -> (x,x))是一个 lambda 表达式,它只是转换成对中的单个字符,因为它们是由自己破译的

-- and creating a map for decryption
cipher :: Map Char Char
cipher = fromList pairs

-- replace encoded text by our cipher
decipher :: String -> String
decipher = mapMaybe (uplook cipher)
         where uplook = flip lookup

result :: String
result = decipher codedMsg

main :: IO ()
main = print result

最后一行打印您的结果 - 因为我们想阅读该消息;-) 如果有不清楚的地方,请随时询问。

PS.:我真的很喜欢你的编码信息——尽管频率分析甚至没有找到一个字母。我只是猜到了你的加密算法。(g?对于 vim 用户),我认为您必须使用更长的文本。

于 2012-06-21T20:30:14.177 回答