Haskell中缀数据类型构造函数的语法规则 [英] Syntax rules for Haskell infix datatype constructors

查看:206
本文介绍了Haskell中缀数据类型构造函数的语法规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个Haskell数据类型有点像一个python字典,一个ruby哈希或一个javascript对象,其中一个字符串链接到一个值,如下:

 数据输入t =输入字符串t 
类型字典t = [Entry t]

上面的代码可以正常工作。但是,我想要一个更好的构造函数,所以我尝试这样定义:

  data Entry t = String〜> t 

此操作失败。我试过这个:

  data Entry t = [Char]〜> t 

同样,它失败了。我知道在Haskell中有特殊的含义,GHCi仍然允许运算符〜> ,但我还是试过另一种方式:

  data Entry t = [Char]& t 

又一次由于解析错误而失败。我发现这个混乱,因为一些莫名的原因,这工作:

  data Entry t = String:>这是否意味着对于在中缀类型构造函数中可能出现什么字符有一定的规则,或者是?它是一种曲解的错误。我不是Haskell的新手,我知道使用第一个构造函数会更加惯用,但这个对我来说,这似乎是我错过了Haskell的一个重要组成部分。 p> 

解决方案

以冒号开头的任何运算符都是类型构造函数或数据构造函数,但( - >)除外。如果你想要波浪号,你可以使用:〜> ,但是你不会使用不以冒号开头的东西。 来源


I'm trying to make a Haskell datatype a bit like a python dictionary, a ruby hash or a javascript object, in which a string is linked to a value, like so:

data Entry t = Entry String t
type Dictionary t = [Entry t]

The above code works fine. However, I would like a slightly nicer constructor, so I tried defining it like this:

data Entry t = String ~> t

This failed. I tried this:

data Entry t = [Char] ~> t

Again, it failed. I know that ~ has special meaning in Haskell, and GHCi still permits the operator ~>, but I still tried one other way:

data Entry t = [Char] & t

And yet another failure due to parse error. I find this confusing because, for some inexplicable reason, this works:

data Entry t = String :> t

Does this mean that there are certain rules for what characters may occur in infix type constructors, or is it a cast of misinterpretation. I'm not a newbie in Haskell, and I'm aware that it would be more idiomatic to use the first constructor, but this one's stumping me, and it seems to be an important part of Haskell that I'm missing.

解决方案

Any operator that starts with a colon : is a type constructor or a data constructor, with the exception of (->). If you want the tilde, you could use :~>, but you're not going to get away with using something that doesn't start with a colon. Source

这篇关于Haskell中缀数据类型构造函数的语法规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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