I want to write a function that checks if two lists are "almost" equal. The first parameter d is used for precision - the difference between the elements must not exceed d.
For example, nearlyEqual 0.5 [2,5] [2.5, 5.1] equals True, but nearlyEqual 0.1 [2,5] [2.5, 5.1] equals False.
I wrote this but it is not working:
nearlyEqual :: Int -> [Int] -> [Int] -> Bool
nearlyEqual d xs ys = foldr(&&) True $ zipWith (\x y -> abs(x-y)<=d)
What am I missing? Any help would be greatly appreciated!
Not sure if it's a typo, but you're not passing
xsandysto your function.should be
at least for it to typecheck.
A clearer implementation would make use of
all, which is of typeFoldable t => (a -> Bool) -> t a -> Bool, and of the function composition operator(.):where
zipWith (-) xs ysis element-wise difference of the two lists, andallverifies that the predicate(<= d) . absholds for all of the elements of that list; the predicate, given an argument, appliesabsto it, and then(<= d)to the result.