Haskell Predicates Using lifters

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}]

About these ads
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s