Search code examples
haskellsyntaxdo-notation

Haskell - How do I nest multiple if statements containing multiple putStrLn satatements


This is the gist of 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"
          }
    }

bonus points for the indication how to add the return statements to that.

working code

This link shows a somewhat working code that allows me to explore problem space using imperative style.


Solution

  • You are missing one semicolon, on line 31, between the putStrLn and an if:

        ....
        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..." )
    
            }
        ....
    

    The template to follow is

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

    Explicit braces and semicolons will prevent any parsing errors due to mis-indentation (possibly due to tabs/spaces issues) in your code.

    The fully explicit notation always uses ; between each of the do block statements. if ... then ... else ... makes up for one expression / do statement, even if spread across several lines of code.