以冒号开头的运算符是构造函数 [英] An operator symbol starting with a colon is a constructor

查看:72
本文介绍了以冒号开头的运算符是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习Haskell.从Haskell 2010文档中:

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.

推荐答案

您可以定义类似

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

GHCi>令v =变量; c =常量
GHCi> c 2:* v"a":+ c 3
(常数2:*变量"a"):+常数3

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.

这篇关于以冒号开头的运算符是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆