Function 这段代码中期望的Haskell反转函数是什么?

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

我正在写一个小的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]
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
)来调用它,另一个答案中也解释了这一点