私は現在、 Haskell Data Analysis の学習で Haskell を学ぼうとしています(Haskell を学ぶなどの他のものを以前に読みました)。本が提案する方法では機能しないcsvデータを解析および変更するためのリストがあります。エラーが発生します。残念ながら、私はリストを修正できるほど上手ではありません。どういうわけかうまくいかないことはわかりますが、正しいバージョンがどのように見えるかわかりません。either
また、関数を使用して正しいバージョンを導出できるかどうかを確認する表現力豊かな例も実際には見つかりませんでした
module LearningDataAnalysis02 where
import Data.List
import Data.Either
import Text.CSV
-- compute the average of List values
average' :: Fractional a => [a] -> a
average' xs = sum xs / genericLength xs
readColumn :: [String] -> [Double]
readColumn xs = map read xs
getColumnInCSV :: CSV -> String -> Either String Integer
getColumnInCSV csv columnName = case lookupResponse of
Nothing -> Left "The column does not exist in this csv file."
Just x -> Right (fromIntegral x)
where
lookupResponse = findIndex (== columnName) (head csv)
applyToColumnInCSV :: ([String] -> b) -> CSV -> String -> Either String b
applyToColumnInCSV func csv column = either
Left
Right . func . elements columnIndex
where
columnIndex = getColumnInCSV csv column
nfieldsInFile = length $ head csv
records = tail $ filter (\record -> nfieldsInFile == length record) csv
elements ci = map (\record -> genericIndex record ci) records
ghci にロードするときに表示されるエラー メッセージは次のとおりです。
LearningDataAnalysis02.hs:22:38:
Couldn't match expected type ‘Either String b’
with actual type ‘a0 -> Either a1 b0’
Relevant bindings include
func :: [String] -> b (bound at LearningDataAnalysis02.hs:22:20)
applyToColumnInCSV :: ([String] -> b)
-> CSV -> String -> Either String b
(bound at LearningDataAnalysis02.hs:22:1)
In the expression: either Left Right . func . elements columnIndex
In an equation for ‘applyToColumnInCSV’:
applyToColumnInCSV func csv column
= either Left Right . func . elements columnIndex
where
columnIndex = getColumnInCSV csv column
nfieldsInFile = length $ head csv
records
= tail $ filter (\ record -> nfieldsInFile == length record) csv
elements ci = map (\ record -> genericIndex record ci) records
LearningDataAnalysis02.hs:24:20:
Couldn't match expected type ‘a0 -> [String]’
with actual type ‘[Field]’
Possible cause: ‘elements’ is applied to too many arguments
In the second argument of ‘(.)’, namely ‘elements columnIndex’
In the second argument of ‘(.)’, namely
‘func . elements columnIndex’
Failed, modules loaded: none.
どなたか親切で、リストを機能させるためにどのように変更しなければならないのか、なぜ本が示唆するように機能しないのかを説明してくれませんか?