Comparing functions in Haskell

前端 未结 5 709
情话喂你
情话喂你 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:38

    1) Your op is overly general. You'll only be using it for Card whatever type rank (undefined :: Card) is, so just make it RankThing -> RankThing. Also, your function type signature is missing a return value type.

    2) An example intended use looks like it would be search succ Ace xs, but that's rather clumsy, how about two auxillary functions that handle the bounds:

    searchUp King _ = []
    searchUp x ys = search succ x ys
    
    searchDown Ace _ = []
    searchDown x ys = search pred x ys
    

    This might read better for your users and avoid the need to check the operation

    3) if you really want to check what operation is performed, and know the operation will be one of two possibilities, then you can either name the operation or test it with a known answer test (KAT). For example:

    data Operation = Succ | Pred
    
    search :: Operation -> Card -> [Card] -> []
    search Succ King _ = []
    search Succ x ys = search' succ x ys
    search Pred Ace _ = []
    search Pred x ys = search' pred x ys
    

    And the KAT solution (rather lame, imo):

    search op x ys
        | op Five == Four && rank x == Ace = []
        | op Five == Six && rank x == King = []
        | otherwise = ...
    

提交回复
热议问题