2016-04-21 94 views
0

我有一個返回IO X(假設X是特定類型)的函數。其中一個參數是輸出目錄的路徑。如果這不存在,它必須被創建。使用IO()操作,但返回IO X

我想使用createDirectoryIfMissing返回IO(),但我不知道如何保持類型一致。我所做的(和失敗)是這樣的:

process :: (Mutation a, Show a, Arbitrary a) 
=> ((a -> BSL.ByteString),(BS.ByteString -> a)) 
-> Bool -> FilePath -> String -> String -> 
Int -> Int -> FilePath -> FilePath -> IO Result 
process (mencode,mdecode) par filename cmd prop maxSuccess maxSize outdir seeds = 
let (prog, args) = (Prelude.head spl, Prelude.tail spl) 
in (case prop of 
    "exec" -> 
     quickCheckWithResult stdArgs { maxSuccess = maxSuccess , maxSize = maxSize, chatty = not par } 
     (noShrinking $ execprop filename prog args mencode outdir) 
    "honggfuzz" -> 
     --do 
     --createDirectoryIfMissing True outdir (This fails) 
     quickCheckWithResult stdArgs { maxSuccess = maxSuccess , maxSize = maxSize, chatty = not par } 
     (noShrinking $ honggprop filename prog args mencode outdir) 

    _  -> process_custom arbitrary (mencode,mdecode) par filename cmd prop maxSuccess maxSize outdir seeds 

) where spl = splitOn " " cmd 

quickCheckWithResult ::可測試道具=>參數數量 - >屬性 - > IO結果 process_custom是基本相同的過程,但與其他參數

編輯:現在實際的代碼,對不起

+2

您沒有看到足夠的代碼來查看您做錯了什麼。 – dfeuer

+0

請至少爲您使用的編譯成功的東西提供類型簽名,以及您堅持使用的完整代碼。 – dfeuer

+0

你的問題不太清楚。你想創建一個目錄並在那之後返回一些特定的內容? – wowofbob

回答

2

例子:

foo :: Int -> IO Int 
foo n = do 
    putStrLn "Hello" -- :: IO() 
    return (n+1)  -- :: IO Int 

do塊中的最後一條語句必須與功能簽名匹配。

或者,使用>>>>=

foo n = putStrLn "Hello" >> return (n+1) 
bar n = getChar >>= \c -> return (fromEnum c) 

都有類型Int -> IO Int