简体   繁体   English

Haskell 从列表中的记录中获取数据

[英]Haskell get data from records in list

I am using haskell and im trying to output all the data from the records that are in the list.我正在使用 haskell,我正在尝试 output 列表中记录中的所有数据。 But i cant figure out how to do it.但我不知道该怎么做。 Image of code代码图像

data Employee = Employee { name :: String,  
                           position :: String,
                           idNum :: Int 
                           } 

samSmith = Employee {name = "Sam Smith", position = "Manager", idNum = 1000}
pamMarx = Employee {name = "Pam Marx", position = "Sales", idNum = 1001}
test = Employee {name = "Test", position = "test", idNum =0}
test2 = Employee {name = "Test2", position = "test2", idNum =01}
test3 = Employee {name = "Test3", position = "test3", idNum =20}
test4 = Employee {name = "Test4", position = "test4", idNum =30}
test5 = Employee {name = "Test5", position = "test5", idNum =40}

testData = [samSmith,pamMarx,test,test2,test3,test4,test5]

renderEmployee :: Employee -> (String,String,Int)
renderEmployee re = (name re,position re,idNum re)

Could you do something like this?你能做这样的事情吗?

data Employee = Employee { name :: String,  
                           position :: String,
                           idNum :: Int 
                            } deriving (Show)
                            

--  Generate a list of employees
employees :: [Employee]
employees = [Employee "John" "Manager" 1,
             Employee "Mary" "Sales" 2,
             Employee "Steve" "Sales" 3,
             Employee "Joe" "Manager" 4,
             Employee "Sally" "Sales" 5,
             Employee "Mark" "Sales" 6,
             Employee "Sally" "Manager" 7]

-- Output the array of employees
printEmployees :: [Employee] -> IO ()
printEmployees [] = putStrLn "No employees"
printEmployees (x:xs) = putStrLn (name x ++ " " ++ position x ++ " " ++ show (idNum x)) >> printEmployees xs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM