Haskell 从列表中的记录中获取数据
Haskell get data from records in list
我正在使用 haskell,我正在尝试从列表中的记录中输出所有数据。但是我不知道该怎么做。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)
你能做这样的事情吗?
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
我正在使用 haskell,我正在尝试从列表中的记录中输出所有数据。但是我不知道该怎么做。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)
你能做这样的事情吗?
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