Why is there a value constructor in addition to the type constructor in Haskell?

后端 未结 3 1028
余生分开走
余生分开走 2021-02-13 06:00

I\'m a newcomer to Haskell and am currently going through Real World Haskell. The book says the type constructor is used only in the type signature while the value constructor i

3条回答
  •  遥遥无期
    2021-02-13 06:45

    Maybe the names are a little bit misleading. A type constructor represents the name of the type you're declaring. They're called like that because they build types, not values: indeed, being (possibly) parameterized on type variables they define a family of types. They act something like C++'s templates and Java's generics. In data MyType a b = Constr a b, MyType is a type constructor that takes two types a and b to build a new type (MyType a b).

    A value constructor is the only part you would call a "constructor" in other (object oriented) languages, because you need it in order to build values for that type. So, in the previous example, if you take the value constructor Constr :: a -> b -> MyType a b, you may build a value Constr "abc" 'd' :: MyType [Char] Char.

提交回复
热议问题