我试图验证一个目录的存在,这样我就可以将它添加到一个有效目录的列表中,但是我得到了与类型不匹配有关的错误。
我的职责是:
--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 <- doesPathExist (显示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发布于 2019-03-24 15:56:51
您的函数mkFileList不能返回[String]类型,而是必须是IO [String]才能与文件系统交互。当您进行这种更改时,您还需要将else fps更改为else return fps。
就目前情况而言,monad推断当您键入do时是list monad,这会传播导致混淆您的错误消息。
https://stackoverflow.com/questions/55325082
复制相似问题