嵌套if / do块中的解析错误

时间:2016-01-28 11:47:19

标签: haskell

defaultFileName :: [Char]
defaultFileName = "Test.log"

defaultSearchName :: String
defaultSearchName = "xyz"

此代码可以编译:

a3 :: Int -> [[Char]] -> IO [Char]
a3 index arg = 
  if null arg
    then do
      a <- putStrLn "No parameters have been passed."
      a <- putStrLn $ "1 Default search string: " ++ defaultSearchName
      a <- putStrLn ("2 Default file name: " ++ defaultFileName)
      return defaultFileName
    else return (arg!!index)

一旦我添加了额外的IF-THEN-ELSE,我就再也无法编译了

a3 :: Int -> [[Char]] -> IO [Char]
a3 index arg = 
  if null arg
    then do
      a <- putStrLn "No parameters have been passed."
      a <- putStrLn $ "1 Default search string: " ++ defaultSearchName
      a <- putStrLn ("2 Default file name: " ++ defaultFileName)
      if index == 0
        then  return defaultSearchName
        else return defaultFileName
    else return (arg!!index)

为什么????每一步都令人沮丧。

1 个答案:

答案 0 :(得分:4)

您的代码编译得很好。你确定你没有将标签和空格作为缩进交错存在吗?

尝试ghci -fwarn-tabs yourfile.hs