newtype Name = Name String deriving (Show)
newtype Age = Age Int deriving (Show)
newtype Weight = Weight Int deriving (Show)
newtype Person = Person (Name, Age, Weight) deriving (Show)
type Gym = [Person]
isAdult :: Person -> Bool
isAdult (Person (_, Age a, _)) = a > 18
w = Person (Name "Lee", Age 30, Weight 120)
p = Person (Name "John" , Age 65, Weight 80)
updateWeight :: Person -> Int -> Person
updateWeight (Person (n, a, Weight w)) b = Person (n, a, Weight $ w + b)
getWeight :: Person -> Int
getWeight (Person (a, b, Weight c)) = c
getAge :: Person -> Int
getAge (Person (a, Age b, c)) = b
我现在正在考虑尝试找出健身房中两个年龄段人群的平均体重。
到目前为止我有
getAverage:: Gym -> (Int, Int) -> Int
getAverage a (b,c) = sum . map getWeight . filter ((b >= age && c <= age) || (b <= age && c >= age))
where
age = getAge a