Haskell-如何嵌套多个包含多个putStrLn满足条件的if语句

时间:2018-08-26 23:53:40

标签: haskell syntax do-notation

这是the problem的要旨:

check_if_proceed configFolders = do
  let emacsdf = "/home/jacek/.emacs.d"
  traceM ("calling check_if_proceed " ++ show ("ccc",configFolders))
  exists <- doesDirectoryExist emacsdf
  symlink <- pathIsSymbolicLink emacsdf
  let confemp = configFolders == []
  let result = False
  if exists
    then do
    {
      if symlink
      then do
        {
          putStrLn ("This action will overwrite the existing symlink\n"++
                    "poinitng to " ++ "SOMEWHERE-FINISH ME\n\n" )
        }
      else do
        {
          putStrLn (emacsdf ++ " is not a symlink\n"++
                    "to use this utility, in your terminal do soemthing like:\n"++
                    "$ mv " ++ emacsdf ++ " " ++ emacsdf ++ "-alternative-config\n" ++
                    "exiting..." )
        }
    }
    else do
    {
      putStrLn ("no " ++ emacsdf ++ "found in your home folder")
      if confemp
      then do
        {
          putStrLn ("nor folders with the alternative emacs config\n" ++
                    "exiting..." )

        }
      else
        do {
          putStrLn "will try to symlink one of the found folders"
          }
    }

加分指示如何在其中添加return语句。

工作代码

此链接显示了一些工作代码,使我可以使用命令式样式探索问题空间。

1 个答案:

答案 0 :(得分:3)

您在putStrLnif之间缺少line 31上的一个分号:

    ....
    else do
    {
      putStrLn ("no " ++ emacsdf ++ "found in your home folder")
    ;                                                            -- HERE
      if confemp
      then do
        {
          putStrLn ("nor folders with the alternative emacs config\n" ++
                    "exiting..." )

        }
    ....

要遵循的模板是

    do { ... ; ... ; ... }

大括号分号将防止由于代码中的缩进(可能是由于制表符/空格问题)而引起的任何解析错误。

完全明确表示法在每个;块语句之间始终使用doif ... then ... else ...弥补了一个表达式/ do语句,即使它们分散在多行代码中。