Home > front end >  Haskell get data from records in list
Haskell get data from records in list

Time:05-01

I am using haskell and im trying to output all the data from the records that are in the list. 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)

CodePudding user response:

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
  • Related