Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard definition of list

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? .

like image 886
877 Avatar asked Apr 08 '11 15:04

877


People also ask

Which is the best definition of a list?

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.

What is the definition of definition list?

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.

What is the definition list in HTML?

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).

What is the use of definition list?

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.


2 Answers

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
like image 76
hugomg Avatar answered Oct 20 '22 21:10

hugomg


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
like image 25
Jeff Foster Avatar answered Oct 20 '22 22:10

Jeff Foster