我学习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'
我为最后的代码行而感到困惑。我为什么得到它?
最佳答案
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