Haskell'doesPathExist'预期类型:布尔,实际类型:IO布尔

时间:2019-03-24 14:56:28

标签: haskell

我正在尝试验证目录的存在,因此可以将其添加到有效目录列表中,但是我遇到有关类型不匹配的错误。

我的功能在这里:

--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

我得到的错误是:

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)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^

编辑:我已经对代码进行了相当大的修改,以使其更加干净,但是在'direx <-dosPathExist(show newp)'上仍然存在相同的错误。

--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

1 个答案:

答案 0 :(得分:1)

您的函数mkFileList不能返回类型[String],而必须是IO [String]才能与文件系统进行交互。进行更改后,您还需要将else fps更改为else return fps

按现状,当您键入do时,monad GHC会推断为列表monad,它会传播,并导致使您感到困惑的错误消息。

相关问题