I have a problem with definition of list. Normally is list defined as data [a] = [] | a : [a]
but if I write something like this on my code concrete I will to define data T a = N | a -> (T a)
the interpreter give me an error:
Malformed head of type or class declaration
Do you know what's wrong? .
a series of names or other items written or printed together in a meaningful grouping or sequence so as to constitute a record: a list of members.
A definition list is a list of terms and corresponding definitions. Definition lists are typically formatted with the term on the left with the definition following on the right or on the next line. The definition text is typically indented with respect to the term.
The <dl> tag defines a description list. The <dl> tag is used in conjunction with <dt> (defines terms/names) and <dd> (describes each term/name).
A definition list is a list of terms (or numbers) and corresponding definitions or descriptions. We like using definition lists in our articles to give readers a clear view of the steps required to complete a task, but just about anything fits in a list. This article will teach you how to use definition lists in Docs.
It looks like your problem is that you tried to use ->
as an infix constructor like :
(In order to build a list using a -> b -> N
syntax). This isn't allowed because custom infix constructors in Haskell must begin with the :
character.
The reason for your strange error message is because ->
in Haskell is reserved for function types, as Jeff's answer explains
Try this instead:
-- Create a right-associative infix constructor.
data T a = N | a :-> (T a)
infixr :->
mylist :: T Int
mylist = 10 :-> 17 :-> N
--If we hadn't made the operator right associative,
-- we would need to use explicit parenthesis here
myotherlist :: T Int
myotherlist = 10 :-> (17 :-> N)
-- Example function
isempty :: T a -> Bool
isempty N = True
isempty (_ :-> _) = False
a -> T a
would mean that a is a function that returns something of T a
so I think that's the bit that's wrong. Try something like this.
data T a = N | R a (T a)
N is the empty list (equivalent of []
) value and R is the value constructor (equivalent to :
)
On the right hand side you need some way of carrying the a
value around. You can now right lists like.
> N -- The empty List
> R 5 N -- a list with a single element and then the end
> R 7 (R 6 (R 5 N)) -- the list 7, 6, 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With