为什么我会收到“不在范围内”的异常消息?

时间:2015-01-14 12:28:44

标签: haskell

我学习Haskell。我的代码:

main = do
  args <- getArgs
  if length args < 2 then 
    putStrLn invalidCallingSignature 
  else
    dispatch fileName command commandArgs
    where (fileName : command : commandArgs) = args -- But I get an Exception: src3.hs:22:48: Not in scope: `args'

我对上次代码行的异常感到困惑。为什么我明白了?

1 个答案:

答案 0 :(得分:7)

where子句适用于整个函数,缩进会误导你。编译器看到的是:

main = do
    args <- getArgs
    if length args < 2 then 
        putStrLn invalidCallingSignature 
    else
        dispatch fileName command commandArgs
  where (fileName : command : commandArgs) = args

因此args不可见。你想要一个记号let

main = do
    args <- getArgs
    if length args < 2 then 
        putStrLn invalidCallingSignature 
    else do
        let (fileName : command : commandArgs) = args
        dispatch fileName command commandArgs