How to compose `not` with a function of arbitrary arity?

前端 未结 4 1588
囚心锁ツ
囚心锁ツ 2020-11-29 01:21

When I have some function of type like

f :: (Ord a) => a -> a -> Bool
f a b = a > b

I should like make function which wrap this

4条回答
  •  死守一世寂寞
    2020-11-29 02:23

    Actually, doing arbitrary arity with type classes turns out to be incredibly easy:

    module Pred where
    
    class Predicate a where
      complement :: a -> a
    
    instance Predicate Bool where
      complement = not
    
    instance (Predicate b) => Predicate (a -> b) where
      complement f = \a -> complement (f a)  
      -- if you want to be mysterious, then
      -- complement = (complement .)
      -- also works
    
    ge :: Ord a => a -> a -> Bool
    ge = complement (<)
    

    Thanks for pointing out this cool problem. I love Haskell.

提交回复
热议问题