我学习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'
我对上次代码行的异常感到困惑。为什么我明白了?
答案 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