此代码中预期的 Haskell 反向函数是什么?
What is the Haskell reverse function expected in this code?
我正在写一个小的Haskell练习,它应该移动列表中的一些元素,类似于凯撒密码,代码已经可以运行,代码如下。
module Lib (shift, cipherEncode, cipherDecode ) where
import Data.Char
import Data.List
import Data.Maybe
abcdata :: [Char]
abcdata = ['a','b','c','d','e','f','g']
iabcdata :: [Char]
iabcdata = ['g','f','e','d','c','b','a']
shift :: Char -> Int -> Char
shift l n = if (n >= 0)
then normalShift l n
else inverseShift l (abs n)
normalShift :: Char -> Int -> Char
normalShift l n = shifter l n abcdata
inverseShift :: Char -> Int -> Char
inverseShift l n = shifter l n reverse(abcdata) -- This is the line
charIdx :: Char -> [Char] -> Int
charIdx target xs = fromJust $ elemIndex target xs
shifter :: Char -> Int -> [Char] -> Char
shifter l n xs = if (n < length (xs))
then
picker ((charIdx l xs) + n) xs
else
picker ((charIdx l xs) + (n `mod` length (xs))) xs
picker :: Int -> [Char] -> Char
picker n xs = if n < length xs
then
xs!!n
else
xs!!(n `mod` length (xs))
我的问题是关于这条线的
inverseShift l n = shifter l n reverse(abcdata)
如果我把它改成
inverseShift l n = shifter l n iabcdata
效果很好
另外,当我 reverse(abcdata) == iabcdata
时,它是 True
但是当我在代码中留下 reverse
时,出现以下错误
* Couldn't match expected type `[Char] -> Char'
with actual type `Char'
* The function `shifter' is applied to four arguments,
but its type `Char -> Int -> [Char] -> Char' has only three
In the expression: shifter l n reverse (abcdata)
In an equation for `inverseShift':
inverseShift l n = shifter l n reverse (abcdata)
|
21 | inverseShift l n = shifter l n reverse(abcdata)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* Couldn't match expected type `[Char]'
with actual type `[a0] -> [a0]'
* Probable cause: `reverse' is applied to too few arguments
我用 reverse(abcdata)
调用 shifter
做错了什么?
括号在 Haskell 中不是这样工作的。按照你写的方式,reverse
和 abcdata
都是 shifter
的参数,但你希望 abcdata
是 reverse
的参数。做 shifter l n (reverse abcdata)
而不是 shifter l n reverse(abcdata)
.
What am I doing wrong by calling shifter
with reverse(abcdata)
?
留言中的答案是:
* Couldn't match expected type `[Char] -> Char'
with actual type `Char'
* The function `shifter' is applied to four arguments,
but its type `Char -> Int -> [Char] -> Char' has only three
In the expression: shifter l n reverse (abcdata)
In an equation for `inverseShift':
inverseShift l n = shifter l n reverse (abcdata)
重复,
In an equation for `inverseShift':
inverseShift l n = shifter l n reverse (abcdata)
~~~~~~~~~~~~~ -- mind the gap!
就是 Haskell 解读你的表情的方式。而这个是你写的:
|
21 | inverseShift l n = shifter l n reverse(abcdata)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
所以你实际上并没有用 reverse(abcdata)
调用 shifter
。
您用 reverse
和 (abcdata)
(以及 l
和 n
)调用它,也有解释在另一个答案中。
我正在写一个小的Haskell练习,它应该移动列表中的一些元素,类似于凯撒密码,代码已经可以运行,代码如下。
module Lib (shift, cipherEncode, cipherDecode ) where
import Data.Char
import Data.List
import Data.Maybe
abcdata :: [Char]
abcdata = ['a','b','c','d','e','f','g']
iabcdata :: [Char]
iabcdata = ['g','f','e','d','c','b','a']
shift :: Char -> Int -> Char
shift l n = if (n >= 0)
then normalShift l n
else inverseShift l (abs n)
normalShift :: Char -> Int -> Char
normalShift l n = shifter l n abcdata
inverseShift :: Char -> Int -> Char
inverseShift l n = shifter l n reverse(abcdata) -- This is the line
charIdx :: Char -> [Char] -> Int
charIdx target xs = fromJust $ elemIndex target xs
shifter :: Char -> Int -> [Char] -> Char
shifter l n xs = if (n < length (xs))
then
picker ((charIdx l xs) + n) xs
else
picker ((charIdx l xs) + (n `mod` length (xs))) xs
picker :: Int -> [Char] -> Char
picker n xs = if n < length xs
then
xs!!n
else
xs!!(n `mod` length (xs))
我的问题是关于这条线的
inverseShift l n = shifter l n reverse(abcdata)
如果我把它改成
inverseShift l n = shifter l n iabcdata
效果很好
另外,当我 reverse(abcdata) == iabcdata
时,它是 True
但是当我在代码中留下 reverse
时,出现以下错误
* Couldn't match expected type `[Char] -> Char'
with actual type `Char'
* The function `shifter' is applied to four arguments,
but its type `Char -> Int -> [Char] -> Char' has only three
In the expression: shifter l n reverse (abcdata)
In an equation for `inverseShift':
inverseShift l n = shifter l n reverse (abcdata)
|
21 | inverseShift l n = shifter l n reverse(abcdata)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* Couldn't match expected type `[Char]'
with actual type `[a0] -> [a0]'
* Probable cause: `reverse' is applied to too few arguments
我用 reverse(abcdata)
调用 shifter
做错了什么?
括号在 Haskell 中不是这样工作的。按照你写的方式,reverse
和 abcdata
都是 shifter
的参数,但你希望 abcdata
是 reverse
的参数。做 shifter l n (reverse abcdata)
而不是 shifter l n reverse(abcdata)
.
What am I doing wrong by calling
shifter
withreverse(abcdata)
?
留言中的答案是:
* Couldn't match expected type `[Char] -> Char'
with actual type `Char'
* The function `shifter' is applied to four arguments,
but its type `Char -> Int -> [Char] -> Char' has only three
In the expression: shifter l n reverse (abcdata)
In an equation for `inverseShift':
inverseShift l n = shifter l n reverse (abcdata)
重复,
In an equation for `inverseShift':
inverseShift l n = shifter l n reverse (abcdata)
~~~~~~~~~~~~~ -- mind the gap!
inverseShift l n = shifter l n reverse (abcdata)
~~~~~~~~~~~~~ -- mind the gap!
就是 Haskell 解读你的表情的方式。而这个是你写的:
|
21 | inverseShift l n = shifter l n reverse(abcdata)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
所以你实际上并没有用 reverse(abcdata)
调用 shifter
。
您用 reverse
和 (abcdata)
(以及 l
和 n
)调用它,也有解释在另一个答案中。