Let’s say that I have a type like this (that works):
data WAE = Num Float
| Id String
| With [(String, WAE)] WAE -- This works, but I want to define it as below
deriving(Eq, Read, Show)
I want a type like this (doesn’t work):
data WAE = Num Float
| Id String
| With [(Id, WAE)] WAE -- This doesn't work ("Id not in scope")
deriving(Eq, Read, Show)
Why can’t I do that in Haskell? Any ideas in achieveing a similar effect?
Based on your question, I’m not entirely sure what you are trying to accomplish. Here are two possibilities that are possible with Haskell’s type-system.
I’m not sure this is what you want, however, because it allows for more than just an id in the first portion of the pair.
Another possibility is to create a new type (or alias) for the id, like so:
Note that MyId could also be created with either
newtypeortype, each giving a slightly different meaning.You cannot, however use
Idas it is a data-constructor, where a type is expected.