I am trying to make my own custom data type in Haskell.
I have the following data types:
type Length = Integer
type Rotation = Integer
data Colour = Colour { red, green, blue, alpha :: Int }
deriving (Show, Eq)
I am trying to make a custom data type that can be either one of the data types above. I have the following:
data Special
= L Length
| R Rotation
| Col Colour
deriving (Show, Eq)
However, I would like to be able to extract the Length
, Rotation
and Colour
value if I have an instance of the Special
data type.
If I had:
L length
Would length
here be of type Special
or of type Length
? If length
is of type Special
is there any way to extract it so it's of type Length
?
For example, is the following code valid?
takeL (x:xs)
| x == (L length) = length
Any insights are appreciated.
For the expression L length
to be valid, length
would have to be a Length
(because L :: Length -> Special
).
takeL (x:xs)
| x == (L length) = length
is not valid. Unless you've redefined length
somewhere, length
is a function [a] -> Int
from the standard library, so L length
is a type error.
I think what you're trying to do here is just pattern matching:
takeL (L length : xs) = length