Define operator using pipe symbol in Haskell

2019-07-21 08:23发布

I would like to know if this can be done:

data NumeroConBase = NumeroConBase { num :: Integer, base :: Integer}

(|) :: String -> Int -> NumeroConBase

Basically, I would like to write 344D|5 and work with that...

I'm getting an error on "|"...

1条回答
干净又极端
2楼-- · 2019-07-21 08:34

| is one of the very few symbol sequences which cannot be used as an operator name in Haskell. The full list is: .., :, ::, =, \, |, <-, ->, @, ~, => and the comment delimiters. All of these have special syntactical meaning. In your case, the lone pipe is used in guards and data declarations.

(Minutiae: the restrictions only apply if the sequences are used on their own; so something like @@ or --> works fine. Also, operators starting with : can only be used as infix constructors in data declarations.)

查看更多
登录 后发表回答