为什么我在myFunction上出现解析错误

时间:2016-10-29 15:02:38

标签: haskell functional-programming parse-error

在下面的代码中,我在输入`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 

2 个答案:

答案 0 :(得分:2)

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 

答案 1 :(得分:0)

在haskell中,执行此操作是不好的做法:

where a = x
  b = y

这可能导致错误,因为它没有排列相同,所以请在以下地址后进行CRNL:

where
  a = x
  b = y