【问题标题】:Haskell - getting Char type when expecting [Char] within List MonadHaskell - 在 List Monad 中期望 [Char] 时获取 Char 类型
【发布时间】:2019-05-02 17:45:52
【问题描述】:

我正在练习 Haskell,尝试编写一个程序来查找 .mp3 和 .flac 元数据并将其整齐地写入文件。我自己已经走了这么远,但我对我应该做的事情感到很困惑。这是这里的主要代码块:

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)
    let tagfiles = mapM (tagsort) filedirs
    putStrLn $ concat listdirs
    putStrLn $ concat tagfiles


tagsort xs = do
    nsartist <- getTags xs artistGetter
    nsalbum  <- getTags xs albumGetter
    artist   <- init $ drop 8 $ show nsalbum
    album    <- init $ drop 7 $ show nsalbum
    (artist ++ " - " ++ album)

我知道,这很混乱。在 ghci 中运行时,我得到了这个:

• Couldn't match expected type ‘[Char]’ with actual type ‘Char’
• In the first argument of ‘(++)’, namely ‘artist’
  In a stmt of a 'do' block: artist ++ " - " ++ album
  In the expression:
    do nsartist <- getTags xs artistGetter
       nsalbum <- getTags xs albumGetter
       artist <- init $ drop 8 $ show nsalbum
       album <- init $ drop 7 $ show nsalbum
       ....
       60    artist ++ " - " ++ album

我无法理解为什么会发生这种情况。在我的测试程序中运行类似的命令:

main = do
artg <- getTags "/home/spilskinanke/backlogtest/02 - await rescue.mp3" artistGetter
let test = init $ drop 8 $ show artg
print test

这工作得很好。在 ghci 中将字符串“65daysofstatic”打印到我的终端。它显然不是 Char 类型。那么为什么在我的代码中被称为 Char 呢? 另请注意,在添加任何引用我正在使用的元数据模块(htaglib)的代码之前,该程序在测试中运行良好。由于没有 tagfiles 函数和 tagsort monad,我能够为某个目录设置一个 arg,我的测试将成功打印一个包含所有可读文件夹的 FilePaths 列表,以及另一个包含所有以我想要的任何文件结尾的文件的 FilePaths 列表mlookup,在本例中为 .mp3、.mp4、.flac 和 .wav。任何帮助将不胜感激。

【问题讨论】:

  • 如果你给每个顶级定义一个类型签名,你会得到更好的错误信息,写出更好的问题。此外,当您只提供一段代码时,请确保 1. 显示您的导入和 2. 提供您编写的在片段中调用/使用但未在其中定义的所有函数/值的类型签名。

标签: haskell io monads


【解决方案1】:

您在tagsort 中混淆了IO 和[]:

tagsort xs = do

    -- Okay, run IO action and bind result to ‘nsartist’
    nsartist <- getTags xs artistGetter

    -- Similarly for ‘nsalbum’
    nsalbum  <- getTags xs albumGetter

    -- Mistaken: ‘init …’ returns a list, not an ‘IO’ action
    artist   <- init $ drop 8 $ show nsalbum
    album    <- init $ drop 7 $ show nsalbum

    -- You are also missing a ‘pure’ or ‘return’ here
    (artist ++ " - " ++ album)

修复很简单:使用let 语句而不是绑定语句&lt;-,并添加一个pure 以在IO String 之外创建一个IO String 你有:

tagsort xs = do
    nsartist <- getTags xs artistGetter
    nsalbum  <- getTags xs albumGetter
    let artist = init $ drop 8 $ show nsalbum
    let album = init $ drop 7 $ show nsalbum
    pure (artist ++ " - " ++ album)

一般来说,每个 do 块必须在一个 monad 中,直到您开始学习使用 monad 转换器来组合不同的效果。因此,在IO 块中,绑定语句右侧的任何内容都必须是IO 动作;如果你只是想做纯粹的计算,你可以使用let(或者只是内联表达式,如果你不需要将某些东西绑定到名称上)。最后,do 块中的最后一条语句也必须是特定 monad 中的一个动作——这通常是一个纯值,只是用 pure :: Applicative f =&gt; a -&gt; f a (或 return :: Monad m =&gt; a -&gt; m a 包裹在 monad 中,它做同样的事情,但由于更严格的Monad 约束,可以在更少的上下文中工作)。

【讨论】:

  • 感谢乔恩的快速回复!我认为是时候休息一下了,因为现在我可以清楚地看到,在我工作的测试程序中,我实际上使用了一个 let 绑定,而我没有在我的主要代码块中使用它。不知道我是怎么错过的!
  • 如果我有另一个与这个问题非常相关的问题,我应该创建一个全新的线程还是直接在这里提问?
  • @Sagarmatha 新问题应该是新问题。
猜你喜欢
  • 1970-01-01
  • 2015-06-08
  • 2014-08-30
  • 1970-01-01
  • 2022-01-23
  • 2021-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多