这是包含一些表格数据的file.csv
文件的摘录
John,23,Paris
Alban,28,London
Klaus,27,Berlin
Hans,29,Stockholm
Julian,25,Paris
Jonathan,26,Lyon
Albert,27,London
此文件的列标题为
firstName, age, city
此文件像{<1}}一样加载
ghci
现在,如果我想在这3列中添加> :m + Data.List Data.Function Data.List.Split
> contents <- readFile "file.csv"
> let t = map (splitOn ",") $ lines contents
> mapM print $ take 3 t
["John","23","Paris"]
["Alban","28","London"]
["Klaus","27","Berlin"]
[(),(),()]
列,我可以
birthYear
这很有效,但令我困扰的是> let getYear str = show $ 2016 - read str
> let withYear = map (\(x:xs) -> x : xs ++ [getYear (head xs)]) t
> mapM print $ take 3 withYear
["John","23","Paris","France","1993"]
["Alban","28","London","UK","1988"]
["Klaus","27","Berlin","Germany","1989"]
[(),(),()]
函数的类型为getYear
,因此,类型检查在这里几乎没用。
我可以轻松地将String -> String
转换为像t
这样的元组列表但是如果我没有3个但却有300个功能(在机器学习问题上并不常见)会怎么样?
("John", 23, "Paris")
,其他列是字符串吗?Int
可以像getYear
(Python示例)?