haskell-绑定映射的monadic函数

问题描述 投票:0回答:1

在此块的代码块中,我有一个函数tagsort,该函数tagsort进行了filepath并返回io字符串。

builddir xs = do writeto <- lastest getArgs let folderl b = searchable <$> (getPermissions b) let filel c = ((lastlookup mlookup c) &&) <$> ((not <$> folderl c)) a <- listDirectory xs listdirs <- filterM (folderl) (map ((xs ++ "/") ++) a) filedirs <- filterM (filel) (map ((xs ++ "/") ++) a) tagfiles <- tagsort <$> filedirs --testprint to terminal putStrLn $ concat listdirs putStrLn $ concat tagfiles tagsort :: Control.Monad.IO.Class.MonadIO m => FilePath -> m [Char] tagsort xs = do nsartist <- getTags xs artistGetter nsalbum <- getTags xs albumGetter let artist = init $ drop 8 $ show nsartist let album = init $ drop 7 $ show nsalbum pure (artist ++ " - " ++ album)
我想掌握此功能并将其映射到目录列表中。运行时,我会得到此错误。

• Couldn't match type ‘[]’ with ‘IO’ Expected type: IO (t0 [Char]) Actual type: [t0 [Char]] • In a stmt of a 'do' block: tagfiles <- tagsort <$> filedirs

我相信我了解这里发生了什么。为了以我希望对标记的方式束缚,我想要一个
IO [String]
,而是将标签映射到列表filedirs会产生

[IO String]

。我不确定如何规避这个问题,或者甚至能够完全绕过它。也许映射不是正确的方法?任何帮助将不胜感激。
    
这是因为该函数

tagsort
haskell io monads
1个回答
2
投票
String -> IO String

注意:我正在使用
IO
为简单起见,并且
String
用于
[Char]
FilePath
如何,将其映射到
filedir :: [String]
使用

(<$>) = fmap :: Functor f => (a -> b) -> f a -> f b

编译器期望在do块中表达的表达方式。

乍一看,这不是很有用。但是,对于此确切的任务,Haskell具有称为
IO [String]

的函数。它的限制现在不需要多大,因为
[IO String]
完全不同。现在,知道它可以是类型

IO [String]


再次令人难以置信,有一个非常有用的预定义效用功能,仅适用于
sequence
最终代码将是:

Foldable

    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.