编辑:我总共犯了4个错误.
在第5行,我有"''",这没有任何意义.
在第9行,我有"map(asciiRotC)".这应该是"map(asciiRotC n)"
在第13行,我有"x:xs.这应该是"(x:xs)".
在最后一行我有"asciiRotWith 0(+1)".这应该是"......(+ 1)0"
这是我的完整代码(该文件名为asciiRot.hs):
import System.Environment
import System.IO
asciiRotC :: Int -> Char -> Char
asciiRotC _ '' = ''
asciiRotC 0 msg = msg
asciiRotC n msg = toEnum (33 + (n + (fromEnum msg) - 33) `mod` 93) :: Char
asciiRot :: Int -> String -> String
asciiRot n msg = map (asciiRotC) msg
asciiRotWith :: (Int->Int) -> Int -> String -> String
asciiRotWith _ _ "" = ""
asciiRotWith f acc x:xs = (asciiRotC acc x) : (asciiRotWith f (f acc) xs)
main = do
args <- getArgs
putStrLn $ asciiRotWith 0 (+1) (head args)
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:asciiRot.hs:8:16:输入`='解析错误
我搜索了类似的错误,发现它们主要与缩进有关或试图在ghci中做事,但事实并非如此,我正在尝试编译,并且我使用空格来缩进.
我试图在第8行添加一个额外的空格,以匹配所有的符号,但错误不会改变.
我正在使用Debian 7,ghc"Glorious Glasgow Haskell编译系统,版本7.4.1".我在vim中编写代码,并使用ghc asciiRot.hs进行编译
早期版本可行.这里是:
asciiRot :: Int -> String -> String
asciiRot _ "" = ""
asciiRot 0 msg = msg
asciiRot n msg = map (\x -> toEnum (33 + (n + (fromEnum x) - 33) `mod` 93) :: Char) msg
Run Code Online (Sandbox Code Playgroud)
这个我将在ghci中运行,其中:l asciiRot.hs