我結束了這個骨架:
f :: (Monad m) => b -> m ()
f x = traverse_ (f . g x) =<< h x -- how avoid explicit recursion?
g :: b -> a -> b
-- h :: (Foldable t) => b -> m (t a) -- why "Could not deduce (Foldable t0) arising from a use of ‘traverse_’"
h :: b -> m [a]
如何避免顯式遞回f?
獎勵:當我嘗試h從[]to概括時Foldable,f不輸入 check ( Could not deduce (Foldable t0) arising from a use of ‘traverse_’) - 我做錯了什么?
更新:這是真正的代碼。Right側面用于遞回名稱為整數的安全攝像機鏡頭的目錄 。Left是處理名稱不是整數的葉子的基本情況。
a <|||> b = left a . right b
doDir (Right d) = traverse_ (doDir . doInt) =<< listDirectory d
where doInt s = ((<|||>) <$> (,) <*> const) (d </> s) $ (TR.readEither :: String -> Either String Int) s
f = doDir并且g ~ doInt進行了一些重構。 h = listDirectory但是當我查看型別模式時
uj5u.com熱心網友回復:
如果您不介意泄漏一些記憶體構建 aTree然后將其丟棄,您可以使用unfoldTreeM:
f = unfoldTreeM (\b -> (\as -> ((), g b <$> as)) <$> h b)
我不相信有一個對應的unfoldTreeM_,但你可以寫一個(使用顯式遞回)。要概括超出Tree/[]連接,您可能還喜歡refoldM; 如果你在 Hackage 上搜索“hylomorphism”,你可以找到幾個類似的函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/487724.html
