notviable
notviable

Reputation: 25

Haskell 'doesPathExist' Expected type: Bool, Actual type: IO Bool

I'm trying to validate a directories existence so I can add it into a list of valid directories, however I'm getting errors regarding type mismatches.

My function is here:

--Make a list of files to work on using a range
mkFileList :: String -> [String] -> [Int] -> [String]
mkFileList fp fps range = do
    let syear = range !! 0
    let smonth = range !! 1
    let sday = range !! 2
    let fyear = range !! 3
    let fmonth = range !! 4
    let fday = range !! 5
    if dateCheck range
      then do newp <- fp ++ "/orders/" ++ (show syear) ++ "/" ++ (show smonth) ++ "/" ++ (show sday)
              direx <- doesPathExist (show newp)

              --Check new directory exists and add if it does
              if direx
                then fps ++ newp
                else do pure ()
              --Construct new variables 
              if (sday+1) > 31
                then do sday <- return (1)
                        if (smonth+1) > 12
                          then do smonth <- return (1)
                                  syear <- return (syear+1)
                                  pure ()
                          else do smonth <- return (smonth+1)
                                  pure ()
                else do sday <- return (sday+1)
                        pure ()
              mkFileList fp fps [syear, smonth, sday, fyear, fmonth, fday]        
      else fps

The error I'm getting is:

Main.hs:141:28: error:
* Couldn't match type `IO' with `[]'
  Expected type: [Bool]
    Actual type: IO Bool
* In a stmt of a 'do' block: direx <- doesPathExist (show newp)
  In the expression:
    do newp <- fp
                 ++
                   "/orders/"
                     ++ (show syear) ++ "/" ++ (show smonth) ++ "/" ++ (show sday)
       direx <- doesPathExist (show newp)
       if direx then fps ++ newp else do pure ()
       if (sday + 1) > 31 then
           do sday <- return (1)
              ....
       else
           do sday <- return (sday + 1)
              ....
       ....
  In a stmt of a 'do' block:
    if dateCheck range then
        do newp <- fp
                     ++
                       "/orders/"
                         ++ (show syear) ++ "/" ++ (show smonth) ++ "/" ++ (show sday)
           direx <- doesPathExist (show newp)
           if direx then fps ++ newp else do ...
           ....
    else
        fps
              direx <- doesPathExist (show newp)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^

EDIT: I've changed my code a fair amount to be somewhat cleaner, still have the same error on 'direx <- doesPathExist (show newp)' however.

--Make a list of files to work on using a range
mkFileList :: String -> [String] -> [Int] -> [String]
mkFileList fp fps range = do
    let syear = range !! 0
    let smonth = range !! 1
    let sday = range !! 2
    let fyear = range !! 3
    let fmonth = range !! 4
    let fday = range !! 5
    if dateCheck range
      then do let newp = fp ++ "/orders/" ++ (show syear) ++ "/" ++ (show smonth) ++ "/" ++ (show sday)
              direx <- doesPathExist (show newp)

              --Check new directory exists and add if it does
              if direx
                then do fps ++ [(show newp)]
                        let nrange = plusOneDay range
                        mkFileList fp fps nrange
                else do let nrange = plusOneDay range
                        mkFileList fp fps nrange      
      else fps

Upvotes: 0

Views: 129

Answers (1)

Thomas M. DuBuisson
Thomas M. DuBuisson

Reputation: 64740

Your function mkFileList can not return type [String] but instead must be IO [String] to interact with the file system. When you make that change you'll also want to change your else fps to else return fps.

As it stands, the monad GHC infers when you type do is the list monad and this propagates to cause an error message that confuses you.

Upvotes: 2

Related Questions