Function 这段代码中期望的Haskell反转函数是什么?
我正在写一个小的Haskell练习,它应该在一个列表中移动一些元素,类似于Caesar密码,代码已经在工作了,代码如下Function 这段代码中期望的Haskell反转函数是什么?,function,haskell,syntax,type-mismatch,function-call,Function,Haskell,Syntax,Type Mismatch,Function Call,我正在写一个小的Haskell练习,它应该在一个列表中移动一些元素,类似于Caesar密码,代码已经在工作了,代码如下 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] iabcd
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))
我用
反向(abcdata)调用移位器
有什么错?在Haskell中括号不是这样工作的。按照您编写的方式,reverse
和abcdata
都是shifter
的参数,但您希望abcdata
成为reverse
的参数。做移位器LN(反向abcdata)
而不是移位器LN反向(反向abcdata)
在Haskell中括号不是这样工作的。按照您编写的方式,reverse
和abcdata
都是shifter
的参数,但您希望abcdata
成为reverse
的参数。执行换档操纵杆LN倒档(abcdata)
而不是换档操纵杆LN倒档(abcdata)
使用reverse(abcdata)
调用shifter
,我做错了什么
答案就在信息中:
* 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)
重复一遍
在“逆移位”的等式中:
inverseShift Ln=换档杆Ln倒档(abcdata)
~~~~~~~~~~~~~~~~~~~-小心空隙代码>
哈斯克尔就是这样解读你的表情的。你是这样写的:
|
21 | inverseShift l n = shifter l n reverse(abcdata)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
因此,实际上您没有使用reverse(abcdata)
调用shifter
您用reverse
和(abcdata)
(以及l
和n
)来调用它,另一个答案中也解释了这一点
使用reverse(abcdata)
调用shifter
,我做错了什么
答案就在信息中:
* 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)
重复一遍
在“逆移位”的等式中:
inverseShift Ln=换档杆Ln倒档(abcdata)
~~~~~~~~~~~~~~~~~~~-小心空隙代码>
哈斯克尔就是这样解读你的表情的。你是这样写的:
|
21 | inverseShift l n = shifter l n reverse(abcdata)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
因此,实际上您没有使用reverse(abcdata)
调用shifter
您用reverse
和(abcdata)
(以及l
和n
)来调用它,另一个答案中也解释了这一点