该代码中预期的Haskell反向函数是什么?           In an equation for `inverseShift':            inverseShift l n = shifter l n reverse (abcdata)            ~~~~~~~~~~~~~ -- mind the gap!

我正在写一个小的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','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

shifter调用reverse(abcdata)是什么意思?

hzhz020261 回答:该代码中预期的Haskell反向函数是什么?           In an equation for `inverseShift':            inverseShift l n = shifter l n reverse (abcdata)            ~~~~~~~~~~~~~ -- mind the gap!

在Haskell中,括号不是这样工作的。编写方式reverseabcdata都是shifter的参数,但是您希望abcdatareverse的参数。用shifter l n (reverse abcdata)代替shifter l n reverse(abcdata)

,
  

shifter调用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)
   |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

因此,您实际上并未用shifter来调用reverse(abcdata)

您使用reverse (abcdata)(还有ln)来调用它,如其他答案中所述。

本文链接:https://www.f2er.com/3126821.html

大家都在问