首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell 'doesPathExist‘期望类型: Bool,实际类型: IO Bool

问Haskell 'doesPathExist‘期望类型: Bool,实际类型: IO Bool
EN

Stack Overflow用户
提问于 2019-03-24 14:56:28
回答 1查看 100关注 0票数 0

我试图验证一个目录的存在,这样我就可以将它添加到一个有效目录的列表中,但是我得到了与类型不匹配有关的错误。

我的职责是:

代码语言:javascript
复制
--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

我得到的错误是:

代码语言:javascript
复制
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 <- doesPathExist (显示newp)‘上仍然有相同的错误。

代码语言:javascript
复制
--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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-24 15:56:51

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

就目前情况而言,monad推断当您键入do时是list monad,这会传播导致混淆您的错误消息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55325082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档