Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell datatype range

Tags:

types

haskell

I define my own datatype.

data Row = A | B | C deriving (Show,Eq,Ord)

The question is, if there is a more elegant way to define my range than this?

instance Ix Row where
  range (A,A) = [A]
  range (A,B) = [A,B]
  range (A,C) = [A,B,C]
  range (B,B) = [B]
  range (B,C) = [B,C]
  range (C,C) = [C]
  range _     = []
like image 222
Ben373 Avatar asked Apr 12 '18 21:04

Ben373


People also ask

How do I check my type in Haskell?

If you need to figure out what the type of an object is in a Haskell program, I hope this is helpful. Note that if you are in GHCI, you can just put :type before your expression to determine the expression's type, or use :set +t to see the type of every expression in GHCI.

Can lists in Haskell have different types?

One is of type (String,Int) , whereas the other is (Int,String) . This has implications for building up lists of tuples. We could very well have lists like [("a",1),("b",9),("c",9)] , but Haskell cannot have a list like [("a",1),(2,"b"),(9,"c")] .

How do you define a type in Haskell?

One introduces, or declares, a type in Haskell via the data statement. In general a data declaration looks like: data [context =>] type tv1 ... tvi = con1 c1t1 c1t2...

How are types used in Haskell?

In Haskell, every statement is considered as a mathematical expression and the category of this expression is called as a Type. You can say that "Type" is the data type of the expression used at compile time. To learn more about the Type, we will use the ":t" command.


1 Answers

There is: Derive Enum and define

range (x, y) = [x .. y]
like image 50
melpomene Avatar answered Sep 24 '22 02:09

melpomene