In Haskell, is there an “elem” method for a constructor with an argument?

Consider the following:

data A = A1 | A2 Integer
x = [A1, A2 999]
elem A1 x == True

Is there a way to do the next test?

elem (A2 _) x == True
+3
source share
6 answers
instance Eq A where
    A1   == A1   = True
    A2 _ == A2 _ = True
    _    == _    = False

elem (A2 undefined) x == True

Of course, this has consequences beyond what you requested.

+1
source

No, but you can rephrase this using any.

hasA2 :: [A] -> Bool
hasA2 = any $ \x -> case x of { A2 _ -> True ; _ -> False }
+8
source

, :

  • .

  • elem , ( ).

Data.List.find:

import Data.List
isA2 A1     = False
isA2 (A2 _) = True
find isA2 [A1, A2 999]
-- => Just (A2 999)

: , , ( , FWIW).

+4

Haskell:

{-# LANGUAGE TemplateHaskell #-}
import Data.ADT.Getters
import Data.Maybe

data A = A1 | A2 Integer
$(mkADTGetters ''A)

hasA2 = any (isJust . gA2)

. Data.ADT.Getters hack- peakachu. ​​ " ", " peakachu ".

+2

:

, , : A2 {}.

hasA2 :: [A] -> Bool
hasA2 = any $ \x -> case x of { A2 {} -> True; _ -> False }

, A2.


ephemient Eq, :

elem (A2 {})

, ( ).

+2

, ! haskell ( ), "Drift", "".

{-! global : is !-}
data Foo = A1 | A2 Int deriving (Show, Eq, Ord)

hasA2 :: [Foo] -> Bool
hasA2 = any isA2

, , pipleine Drift, * :

{-* Generated by DrIFT : Look, but Don't Touch. *-}
isA1 (A1) = True
isA1 _ = False
isA2 (A2 _) = True
isA2 _ = False

"getive". , {-!...! -} :

{-!
deriving instance Is Foo
!-}

:

data Foo = A1 | A2 Int deriving (Show, Eq, Ord {-! Is !-})

"" :

isA1 :: Foo -> Bool
isA1 (A1{}) = True
isA1 _ = False

isA2 :: Foo -> Bool
isA2 (A2{}) = True
isA2 _ = False

derive Hackage, DrIFT .

+1

Source: https://habr.com/ru/post/1730347/


All Articles