字符串的Haskell函数头作为函数的输入

我在Haskell中创建了一个Enigma Machine,我对以下代码有疑问:

encrypt :: String -> [String] -> String
encrypt t [d1,d2,d3]
  | length t > 1 = encryptOne head t [d1,d3] ++ encrypt (tail t) [d1,rotateF d3]
  | length t == 1 = encryptOne head t [d1,d3]
  | otherwise = ""

加密函数采用字符串,并使用encryptOne函数递归地求解每个字符。每次迭代,都使用rotateF函数旋转转子(早期阶段,仍在解决这个问题)。 encryptOne函数如下所示:

encryptOne :: Char -> [String] -> Char

问题是encryptOnet [d1,d3]部分,其中头t无法解决,并且发生以下错误

    length t == 1 = encryptOne head t [d1,d3]
                               ^^^^
    * Couldn't match type `Char' with `[Char]'
      Expected type: [String]
        actual type: String

有人知道如何重构此部分,以便头t返回要在encryptOne中使用的字符吗?

lixiaoxuaner 回答:字符串的Haskell函数头作为函数的输入

多亏了Willem我使用括号找到了第一个问题。

第二个问题是有关encryptOne的类型问题。我现在将其添加到问题中,它表明它正在返回Char,而crypto函数返回String。我必须将encryptOne的结果从Char转换为String才能解决:

charToString :: Char -> String
charToString c = [c]
本文链接:https://www.f2er.com/3047890.html

大家都在问