【发布时间】: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. 提供您编写的在片段中调用/使用但未在其中定义的所有函数/值的类型签名。