Checking for a particular data constructor

后端 未结 3 1058
一整个雨季
一整个雨季 2020-12-18 21:26

Let\'s say that I defined my own data-Type like

data MyData = A arg| B arg2| C arg3

How would I write a function (for instance: isMy

相关标签:
3条回答
  • 2020-12-18 21:32

    I believe you want functions to test for particular constructors:

    isA :: MyData -> Bool
    isB :: MyData -> Bool
    

    If so, then you can write these yourself or derive them. The implementation would look like:

    isA (A _) = True
    isA _     = False
    
    isB (B _) = True
    isB _     = False
    

    To derive them automatically, just use the derive library and add, in your source code:

    {-# LANGUAGE TemplateHaskell #-}
    import Data.DeriveTH
    
    data MyData = ...
        deriving (Eq, Ord, Show}
    
    derive makeIs ''MyData
    -- Older GHCs require more syntax: $( derive makeIs ''MyData)
    

    Also note: your data declaration is invalid, the name must be capitalized, MyData instead of myData.

    Finally, this whole answer is based on the assumption you want to test constructors, not data types as you said (which are statically checked at compile time, as Tarrasch said).

    0 讨论(0)
  • 2020-12-18 21:52

    You can use Maybes. You can create a set of functions that check for each of the types

    getA, getB, getC :: MyData a -> Maybe a
    getA x = case x of {(A v) -> Just v; _ -> Nothing}
    getB x = case x of {(B v) -> Just v; _ -> Nothing}
    getC x = case x of {(C v) -> Just v; _ -> Nothing}
    

    This affords some practical idioms for certain tasks:

    allAs :: [MyData a] -> [a]
    allAs xs = mapMaybe getA xs
    
    printIfA :: Show a => MyData a -> IO ()
    printIfA x = maybe (return ()) print $ getA x
    
    0 讨论(0)
  • 2020-12-18 21:57

    Haskell always checks that the types makes sense. The compiler would complain immediately if you wrote isMyDataType 4, because 4 is not of type MyData, it's of type Int.

    I'm not sure this is what you asked for, but either way I strongly suggest for you to try out what you've asked here in practice, so you can see for yourself. Most important is that you check out type signatures in haskell, it is key for learning haskell.

    0 讨论(0)
提交回复
热议问题