我正在解决基本加密问题。我试图创建一个名为encryptf
的函数
到目前为止看起来像这样:
encryptf :: Eq a => [a] -> [a] -> (a -> a)
encryptf xs ys = \f -> head [d | (c,d) <- (zip xs ys)]
然后有一个名为encryptString
的第二个函数,如下所示:
encryptString :: (a -> a) -> [a] -> [a]
encryptString f xs = map f xs
这些函数将像这样使用:
encryptString (encryptf "abc" "xyz") "dcba"
然后会返回"dzyx"
现在我知道我的encryptf
函数是错误的,因为它会将所有内容都更改为ys的第一个元素。我不确定从哪里开始。任何帮助将不胜感激。
答案 0 :(得分:0)
这个想法是你在压缩列表中搜索给定的字符,如果它在那里然后返回第二对,如果没有,则返回字符本身。
encryptf :: Eq a => [a] -> [a] -> (a -> a)
encryptf xs ys =
let ret = zip xs ys in
\f ->
case lookup f ret of
Just c -> c
Nothing -> f
encryptString :: (a -> a) -> [a] -> [a]
encryptString f xs = map f xs
*Main> encryptString (encryptf "abc" "xyz") "dcba"
"dzyx"