Matching polymorphic data in Haskell

José Ricardo Ribeiro

Lets say we have a data defined named Stuff:

data Stuff = Stuff1 Int
           | Stuff2 Int
           | Stuff3 Int

sumStuff :: [Stuff] -> Int
sumStuff [] = 0
sumStuff ((Stuff1 x):xs) = x + sumStuff xs
sumStuff ((Stuff2 x):xs) = x + sumStuff xs
sumStuff ((Stuff3 x):xs) = x + sumStuff xs

sumStuff' :: [Stuff] -> Int
sumStuff' [] = 0
sumStuff' ((_ x):xs) = x+sumStuff xs

How can I match all types without pattern matching like in the wrong definition in sumStuff' ?

Thank you in advance!

bheklilr

You could take a different approach with your data structure if it's as homogenous as your example:

data StuffType = Stuff1 | Stuff2 | Stuff3 deriving (Eq, Show)

data Stuff a = Stuff StuffType a deriving (Eq, Show)

extractStuff :: Stuff a -> a
extractStuff (Stuff _ a) = a

sumStuff :: Stuff Int -> Int
sumStuff = sum . map extractStuff

I've even made the value contained in Stuff polymorphic, in case you wanted to store Strings or even more Stuffs in them. This approach allows you to pattern match on the StuffType when you need it, but stick with a single pattern case when you don't.

You could also define this using records to avoid pattern matches altogether:

data Stuff a = Stuff { stuffType :: StuffType, extractStuff :: a } deriving (Eq, Show)

and sumStuff would have the same definition, but you wouldn't need to define extractStuff manually.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can I constrain the parametric polymorphic type on type/data constructor in Haskell?

From Dev

Haskell polymorphic function to convert between algebraic data types

From Dev

Haskell: Using patten matching with currying data

From Dev

List of polymorphic functions in haskell?

From Dev

Haskell - Illegal Polymorphic type?

From Dev

haskell polymorphic type functions

From Dev

Polymorphic type aliases in haskell

From Dev

Confused about Haskell polymorphic types

From Dev

haskell polymorphic function evaluation error

From Dev

Haskell - deriving show for polymorphic types

From Dev

Is there a default polymorphic unit type haskell

From Dev

Polymorphic functions in Record Types Haskell

From Dev

haskell polymorphic function evaluation error

From Dev

Haskell - Functor instance for generic polymorphic Algebraic Data Types using recursion-schemes

From Dev

Functions to Polymorphic data types

From Dev

FORTRAN: data polymorphic

From Dev

Polymorphic query in spring data

From Dev

Haskell linear algebra libraries that are polymorphic with classes of kind *

From Dev

Lack of polymorphic inference in haskell when a binding is absent

From Dev

Is there an automatic way to memoise global polymorphic values in Haskell?

From Dev

Does Haskell support closed polymorphic types?

From Dev

Writing a polymorphic Haskell function with ST array

From Dev

Functor instance for generic polymorphic ADTs in Haskell?

From Dev

Polymorphic variable used in different contexts haskell

From Dev

reuse/memoization of global polymorphic (class) values in Haskell

From Dev

How to explicitly instantiate/specialise a polymorphic Haskell function?

From Dev

Haskell polymorphic function Using Either Left Right

From Dev

Can Haskell display an object of polymorphic type ?

From Dev

Haskell pattern matching on vectors

Related Related

  1. 1

    Can I constrain the parametric polymorphic type on type/data constructor in Haskell?

  2. 2

    Haskell polymorphic function to convert between algebraic data types

  3. 3

    Haskell: Using patten matching with currying data

  4. 4

    List of polymorphic functions in haskell?

  5. 5

    Haskell - Illegal Polymorphic type?

  6. 6

    haskell polymorphic type functions

  7. 7

    Polymorphic type aliases in haskell

  8. 8

    Confused about Haskell polymorphic types

  9. 9

    haskell polymorphic function evaluation error

  10. 10

    Haskell - deriving show for polymorphic types

  11. 11

    Is there a default polymorphic unit type haskell

  12. 12

    Polymorphic functions in Record Types Haskell

  13. 13

    haskell polymorphic function evaluation error

  14. 14

    Haskell - Functor instance for generic polymorphic Algebraic Data Types using recursion-schemes

  15. 15

    Functions to Polymorphic data types

  16. 16

    FORTRAN: data polymorphic

  17. 17

    Polymorphic query in spring data

  18. 18

    Haskell linear algebra libraries that are polymorphic with classes of kind *

  19. 19

    Lack of polymorphic inference in haskell when a binding is absent

  20. 20

    Is there an automatic way to memoise global polymorphic values in Haskell?

  21. 21

    Does Haskell support closed polymorphic types?

  22. 22

    Writing a polymorphic Haskell function with ST array

  23. 23

    Functor instance for generic polymorphic ADTs in Haskell?

  24. 24

    Polymorphic variable used in different contexts haskell

  25. 25

    reuse/memoization of global polymorphic (class) values in Haskell

  26. 26

    How to explicitly instantiate/specialise a polymorphic Haskell function?

  27. 27

    Haskell polymorphic function Using Either Left Right

  28. 28

    Can Haskell display an object of polymorphic type ?

  29. 29

    Haskell pattern matching on vectors

HotTag

Archive