你好我想這個字符串將字符串轉換爲自定義類型的Haskell
Blade Runner,Ridley Scott,1982,Amy,5,Bill,8,Ian,7,Kevin,9,Emma,4,Sam,7,Megan,4
轉換爲電影類型
type UserRatings = (String,Int)
type Film = (Title, Director, Year , [UserRatings])
從包含25部電影
這個文本文件是我試圖做的
maybeReadTup :: String ->(String, Int)
maybeReadTup s = do
[(n, [c])] <- return $ reads s
return [(n, [c])]
parseLines :: [String] -> Film
parseLines list
| isInt(list !! 3) = (list !! 0,(list !! 1), read (list !! 2), maybeReadTup [ (list!!1,read (list !! 2))])
isInt :: String ->Bool
isInt[] = True
isInt (x:xs)
| isNumber x = True && isInt xs
| otherwise = False
parseChars :: String -> String -> [String]
parseChars [] _ = []
parseChars (x:xs) stringCount
| x == ',' = [stringCount] ++ parseChars xs ""
| otherwise = (parseChars xs (stringCount ++ [x]))
parseAll :: [String] -> [Film]
parseAll [] = []
parseAll (x:xs) = parseLines (parseChars x "") : (parseAll xs)
但我得到錯誤的類型可以有人請幫我解析這個UserRatings元組類型[(String,Int)]
?你能幫我理解parseLines的工作原理嗎?我在Haskell
爲什麼在輸入每一個字,用逗號隔開? – chepner
@chepner有沒有更好的方法來做到這一點?如果我改變這個文件,它會幫助我解析它嗎? – Max
我想象一下像'Blade Runner,Ridley Scott,1982,...'這樣的東西會更好,這樣你就可以知道標題結尾和導演開始的位置(當然,假設標題不包含逗號)。 – chepner