1

I have to a data Student and a type Class:

data Student = Student {nome :: String
                        , stdNumber :: Int
                        , approvedClass :: Int
                        , failedClass :: Int
                        }
type Class = [Student]

and I'm trying do add to the approvedClass number and failed of a Student but i'm don't know how to do it . I already have this:

addClasses :: Student-> Int -> Int -> Student
addClasses student aC fC = (student _ _ (approvedClass+aC) (faileClass+fC))

But it doesn't work and i can't understand why? or how to make it work?

one user
  • 109
  • 9

2 Answers2

7

_ is only valid in pattern matching, and does not automatically denote "keep this field the same". Instead, use record update syntax:

addClasses :: Student-> Int -> Int -> Student
addClasses student aC fC =
    student { approvedClass = approvedClass student + aC
            , failedClass = failedClass student + fC
            }
Aplet123
  • 33,825
  • 1
  • 29
  • 55
5

As an alternative to record syntax, you would need to match each field explicitly to reuse in constructing a new Student value.

addClasses :: Student -> Int -> Int -> Student
addClasses (Student w x y z) aC fC = Student w x (y + aC) (z + fC)
chepner
  • 497,756
  • 71
  • 530
  • 681