joy of lifters in haskell :
code below create predicate for Person data, later used in filtering :
data Person = Person {weight::Double,height::Double}
deriving Show
data PersonP’ a = PersonP’ {unconP::Person -> a}
getWeight = PersonP’ (\(Person x y)-> x)
getHeight = PersonP’ (\(Person x y)-> y)
–closure usage
consP’ k = PersonP’ (\_-> k)
liftP’:: (a->b->c) -> PersonP’ a -> PersonP’ b -> PersonP’ c
liftP’ q (PersonP’ f ) (PersonP’ g ) = PersonP’ h
where h person = f person `q` g person
weightP’ q k = liftP’ q getWeight (consP’ k)
heightP’ q k = liftP’ q getHeight (consP’ k)
andP’ f g = liftP’ (&&) f g
orP’ f g = liftP’ (||) f g
–finding person with weight between 10 and 15
filter (unconP (weightP’ (>) 10 `andP’` weightP’ () 15 `orP’` heightP’ (==) 25 ) ) [Person 10 12,Person 20 30,Person 13 25]
[Person {weight = 20.0, height = 30.0},Person {weight = 13.0, height = 25.0}]