1

Hey I am working on my first ever Haskell project which is a project I have completed in both Python and Java. I am making a simulation for an NHL season and throughout the season I need to update teams win/loss records. Below is a function that I believe works properly to update the loss parameter of my structured data but it just seems wrong. I feel like the function below is not very...functional and I am wondering if I am still thinking imperatively with this implementation if that makes sense. I just have a feeling that there is a better way to do this though I could be wrong. Any advice would be awesome.

data Team = Team { teamName :: String  
                 , wins :: Int  
                 , losses :: Int  
                 , overtimeLosses :: Int  
                 , points :: Int  
                 } deriving (Show)


giveLoss :: Team -> Team
giveLoss t = Team{teamName = teamName t, wins = wins t, losses = losses t+1
                  ,overtimeLosses = overtimeLosses t, points = points t}
LeapyNick
  • 103
  • 6

1 Answers1

3

Use record update syntax:

giveLoss t = t{losses = losses t+1}

You can also pattern-match instead of using the record selector function if you want:

giveLoss t{losses = x} = t{losses = x+1}