为什么我在 myFunction 上收到解析错误

Why am I getting parse error on myFunction

在以下代码中,我在输入“myFunction”时遇到解析错误:

import System.Environment (getArgs)
interactWith function inputFile outputFile = do  
    input <- readFile inputFile writeFile outputFile (function input)
main = mainWith myFunction  
 where mainWith function = do         
        args <- getArgs          
        case args of            
            [input,output] -> interactWith function input output         
            _ -> putStrLn "error: exactly two arguments needed"
        -- replace "id" with the name of our function below       
        myFunction = id 

myFunction = id 需要与 where 子句中定义的其他内容对齐。

main = mainWith myFunction  
 where mainWith function = do         
     ^  args <- getArgs          
     |  case args of            
     |      [input,output] -> interactWith function input output         
     |      _ -> putStrLn "error: exactly two arguments needed"
     | -- replace "id" with the name of our function below       
     | myFunction = id 
     | ^
     | |
     | here
     |
     not here

相同的代码,多缩进以使其更清晰:

import System.Environment (getArgs)
interactWith function inputFile outputFile = do  
    input <- readFile inputFile 
    -- I assume a line break belongs here 
    writeFile outputFile (function input)
main = mainWith myFunction  
    where mainWith function = do         
              args <- getArgs          
              case args of            
                  [input,output] -> interactWith function input output         
                  _ -> putStrLn "error: exactly two arguments needed"
          -- replace "id" with the name of our function below       
          myFunction = id 

在 haskell 中,这样做是不好的做法:

where a = x
  b = y

这可能会导致错误,因为它排列不一样,所以请在 where 之后创建一个 CRNL:

where
  a = x
  b = y