How can I define a new data type that is a list of numbers?

Max

I am a complete Haskell n00b, but I would like to define a new data type that is simple a list of numbers. How would I go about doing this? I've read Haskell wikibook on type declarations, as well as other online resources, but I cannot seem to figure it out. Here is, in essence, what I've tried:

type NumList = [Num]

That didn't work, so how can I do this? Thanks for the help.

gregoryck

The type keyword is just for type synonyms (new names of types that already exist), so you can't use a class like Num.

Instead, you might use the data keyword together with Haskell's context notation:

data Num a => NumList a = NumList [a]

Except when I try that in ghci, it scolds me because datatype contexts are deprecated. Apparently you're better off using a GADT. Perhaps something like:

data NumList a where
    Empty :: Num a => NumList a
    Singleton :: Num a => a -> NumList a
    Append :: Num a => NumList a -> NumList a -> NumList a

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I generate a list of consecutive numbers?

From Dev

How can I define Content-type in Swift using NSURLSession

From Dev

How can I define an Elisp widget type that only accepts positive numbers?

From Dev

How do I define a data type that only accepts numbers?

From Dev

Do I have to define a function for each new data type?

From Dev

How to define data type?

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define new generic list of objects in Swift?

From Dev

How can I define type variant in c#?

From Dev

How can I define NFData instance for recursive singleton type?

From Dev

How can I define a new operator in Prolog?

From Dev

Can I define a structure data type in CMake?

From Dev

How do I define a custom slot type that isn't a list?

From Dev

How I can define a list of map::iterator and map of list::iterator

From Dev

How can I define a property type as being a list (list, set, array, collection) of string in my YAML Swagger definition

From Dev

How can I add new item in a list?

From Dev

Typescript - How can I conditionally define a type based on another property

From Dev

How can I put these numbers in list?

From Dev

How to define data type?

From Dev

Can I define a "factor" data type for a field?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I create a data.frame from a nested list with differing numbers of variable

From Dev

How can I square the list of numbers in a dictionary?

From Dev

How can I create a list of numbers in a combobox?

From Dev

How can I define NFData instance for recursive singleton type?

From Dev

How do i append a list with new data?

From Dev

How can I define a property type as being a list (list, set, array, collection) of string in my YAML Swagger definition

From Dev

How to assign numbers to a list of returned python data that I can call on to print

Related Related

  1. 1

    How can I generate a list of consecutive numbers?

  2. 2

    How can I define Content-type in Swift using NSURLSession

  3. 3

    How can I define an Elisp widget type that only accepts positive numbers?

  4. 4

    How do I define a data type that only accepts numbers?

  5. 5

    Do I have to define a function for each new data type?

  6. 6

    How to define data type?

  7. 7

    How can I define a return type of void for a function in a Typescript interface?

  8. 8

    How can I define the return type of a lodash reduce function with Typescript?

  9. 9

    How can I define new generic list of objects in Swift?

  10. 10

    How can I define type variant in c#?

  11. 11

    How can I define NFData instance for recursive singleton type?

  12. 12

    How can I define a new operator in Prolog?

  13. 13

    Can I define a structure data type in CMake?

  14. 14

    How do I define a custom slot type that isn't a list?

  15. 15

    How I can define a list of map::iterator and map of list::iterator

  16. 16

    How can I define a property type as being a list (list, set, array, collection) of string in my YAML Swagger definition

  17. 17

    How can I add new item in a list?

  18. 18

    Typescript - How can I conditionally define a type based on another property

  19. 19

    How can I put these numbers in list?

  20. 20

    How to define data type?

  21. 21

    Can I define a "factor" data type for a field?

  22. 22

    How can I define the return type of a lodash reduce function with Typescript?

  23. 23

    How can I create a data.frame from a nested list with differing numbers of variable

  24. 24

    How can I square the list of numbers in a dictionary?

  25. 25

    How can I create a list of numbers in a combobox?

  26. 26

    How can I define NFData instance for recursive singleton type?

  27. 27

    How do i append a list with new data?

  28. 28

    How can I define a property type as being a list (list, set, array, collection) of string in my YAML Swagger definition

  29. 29

    How to assign numbers to a list of returned python data that I can call on to print

HotTag

Archive