Comparing functions in Haskell

前端 未结 5 721
情话喂你
情话喂你 2020-11-30 13:48

Is there any way to compare two functions in Haskell?

My thought is that the answer is no since functions would not derive the Eq type class. However I\'m trying to

5条回答
  •  盖世英雄少女心
    2020-11-30 14:25

    The code below may not compile, but hopefully you get the idea:

    data Op = Succ | Pred deriving Eq
    
    fromOp :: Enum a => Op -> a -> a
    fromOp Succ = succ
    fromOp Pred = pred
    
    search :: Op -> Card -> [Card] -> [Card]
    search op x list = if (op == Succ && rank x == King) || 
                          (op == Pred && rank x == Ace)
                       then []
                       else let c = [ n | n <- list, rank n == (fromOp op) (rank x)]
                         in if length c == 1
                         then x : search op (head c) list
                         else []
    

提交回复
热议问题