Reputation: 134
Say I have two datatypes look like this:
data DataType1 = DataType1 { id :: Int, values :: [Int]} deriving (Show)
data DataType2 = DataType2 { dataType1Id :: Int, values2 :: [Int]} deriving (Show)
Now I want to compare the average of values and the related values2 (so where DataType1Id = id). I am pretty new in Haskell so I don't know if this is possible.
This is my function to compare the averages:
isGreaterAvgThen :: DataType2 -> Bool
isGreaterAvgThen x
| average (values2 x) > average (values y) = True
| otherwise = False
where y = ...
average is a function that calculates the average of a list of Ints (average :: [Int] -> Double)
Upvotes: 0
Views: 243
Reputation: 12070
Would this work for you?
where y = fromMaybe (error "missing value") $
find ((== id x) . dataType1Id) listOfDataType2
Since the list might be missing the desired value, the type of find
is wrapped in Maybe.... If you are sure that it is there, you can unwrap it with fromMaybe
like I did, but be warned, error
is just another way to say "crash"....
Upvotes: 2