Fre*_*rez 1 syntax haskell function function-call type-mismatch
我正在写一个小的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))
Run Code Online (Sandbox Code Playgroud)
我的问题是关于生产线
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
Run Code Online (Sandbox Code Playgroud)
什么我通过调用做错了shifter用reverse(abcdata)?
括号在Haskell中不是这样的。你的方式写的,reverse并且abcdata都希望能参数shifter,但你要abcdata成为一个参数reverse。做shifter l n (reverse abcdata)代替shifter l n reverse(abcdata)。