我在 Haskell 中的工作以在 Haskell 中重新处理 .Net F# 项目的形式来获得乐趣。
我正在解析一个常规的 Windows 配置文件——每行一个键/值对,键与值由=
. 这个文件非常简单明了,这让我的解析代码简单明了,我喜欢。
我的问题是为什么部分应用程序不适用于下面的最后一行代码。它显然在先前的行和其他功能中工作。
module Configuration (Config (..), load) where
import Data.Char (isSpace)
import Data.List (isPrefixOf)
data Config = Config { aliases :: [String]
, headers :: [String] }
-- This is a naive implementation. I can't decide if I like it better than
-- trim = unpack . strip . pack.
trim :: String -> String
trim = reverse . dropSpaces . reverse . dropSpaces
dropSpaces :: String -> String
dropSpaces = dropWhile isSpace
split _ [] = []
split c cs = [takeWhile (/= c) cs] ++ split c (tail' $ dropWhile (/= c) cs)
where tail' [] = []
tail' (x:xs) = xs
load :: String -> Config
load text =
let ss = lines text
hs = map getValue $ getLines "Header" ss
as = split ',' $ getValue $ getLine "AliasList" ss
in Config { aliases=as, headers=hs }
where getLines p = filter (p `isPrefixOf`)
getValue = trim . drop 1 . dropWhile (/= '=')
getLine = head . getLines -- Why isn't partial application working here?
我得到的错误如下:
Configuration.hs:30:29:
Couldn't match expected type `[c0]'
with actual type `[[a0]] -> [[a0]]'
Expected type: [a0] -> [c0]
Actual type: [a0] -> [[a0]] -> [[a0]]
In the second argument of `(.)', namely `getLines'
In the expression: head . getLines
谢谢!