Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An operator symbol starting with a colon is a constructor

Tags:

haskell

I learn Haskell. From Haskell 2010 documentation:

  • An operator symbol starting with a colon is a constructor.
  • An operator symbol starting with any other character is an ordinary identifier.

I don't understand first phrase. I know exist data constructors and class type constructors. What constructor this case? Maybe I need a code sample.

like image 848
Andrey Bushman Avatar asked Jan 22 '15 11:01

Andrey Bushman


People also ask

What does a single colon mean in Haskell?

In Haskell, the colon operator is used to create lists (we'll talk more about this soon). This right-hand side says that the value of makeList is the element 1 stuck on to the beginning of the value of makeList .

Which symbols can be used as an identifier?

Identifiers contain characters from any of: alpha, digit, underscore, and dollar sign. You can't use spaces or tabs or symbols like #, @, !, and so forth in an identifier.

What are identifiers in Haskell?

An identifier starts with a letter or underscore and consists of: Letters. Numbers. _ (underscore) ' (single quote, sometimes pronounced as “prime”)


2 Answers

You can define stuff like

data Symbolic n
   = Constant n
   | Variable String
   | Symbolic n :+ Symbolic n
   | Symbolic n :* Symbolic n
  deriving (Show)

GHCi> let v = Variable; c = Constant
GHCi> c 2 :* v"a" :+ c 3
    (Constant 2 :* Variable "a") :+ Constant 3

That's what the first phrase refers to.

like image 170
leftaroundabout Avatar answered Sep 24 '22 17:09

leftaroundabout


I know exist data constructors and class type constructors. What constructor this case?

In standard Haskell only data constructors can be symbolic and type names must be alphanumeric. If you enable the GHC extension TypeOperators, type names can be symbolic as well, allowing you to define type constructors that start with :.

like image 21
sepp2k Avatar answered Sep 24 '22 17:09

sepp2k