peroni_santo
peroni_santo

Reputation: 367

type variables in constructors?

Can I have a function in a data constructor? Like:

data Something = (a->b) Something1 Something2

Upvotes: 4

Views: 183

Answers (2)

fuz
fuz

Reputation: 92976

Yeah, of course you can. The only important thing is that you (always) need a name for your data constructor:

data <name> <para0> <param1> ... = <constructor> <arg0> <arg1> <arg2> ...

So for our example, it becomes

data Something a b = Constructor (a -> b) Something1 Something2

Upvotes: 12

Satvik
Satvik

Reputation: 11208

There are some rules to be followed in naming of a Constructor.

  • Start with a uppercase letter.
  • Can contain underscores, single quotes, letters and digits.
  • Constructors can be operator names, so long as they start with a ':' .

But ofcourse you can have functions in a data definition like

data Something a b = Something (a->b) a b 

Upvotes: 5

Related Questions