简体   繁体   English

Haskell无效类型签名

[英]Haskell invalid type signature

Quick question, what is wrong with this? 快问,这有什么问题?

(get) :: [a] -> Int -> a   -- <- line 21
(x:xs) get 0 = x
(x:xs) get (n+1) = xs get n

ghci gives this error when I try to load the file that contains that code. 当我尝试加载包含该代码的文件时,ghci给出了此错误。

Prelude> :load ch6.hs 
[1 of 1] Compiling Main             ( ch6.hs, interpreted )

ch6.hs:21:0: Invalid type signature
Failed, modules loaded: none.

I'm trying to make get an infix operator. 我正试图让一个中缀运算符。

You shouldn't have parentheses around get , to start with. 你不应该加上括号get ,开始。 The syntax for the whole definition looks a bit off, though. 但是,整个定义的语法看起来有点过时了。 I'm guessing you wanted something like this: 我猜你想要这样的东西:

get :: [a] -> Int -> a
get (x:xs) 0 = x
get (x:xs) (n+1) = xs `get` n

Note the backticks around get in order to use it infix, which is necessary here because the rules for an alphanumeric identifier are different from operators: Operators are made of symbols, are are infix by default, and to write them without arguments or use them prefix, you put them in parentheses. 请注意,反引号各地get以缀使用它,因为一个字母数字标识符的规则,从运营商不同的是需要在这里:运营商是由符号,是中缀在默认情况下,并把它们写不带参数或使用它们的前缀,你把它们放在括号中。 Alphanumeric identifiers are prefix by default, and surrounding them with backticks lets you use them infix. 默认情况下,字母数字标识符是前缀,并且使用反引号包围它们可让您使用它们作为中缀。

You can use the backticks on the left-hand side as well, if you want, but that looks a bit odd to my eye: 如果你愿意的话,你也可以在左侧使用反引号,但这看起来有些奇怪:

(x:xs) `get` 0 = x
(x:xs) `get` (n+1) = xs `get` n

Incidentally, the pattern syntax n+1 is deprecated, so you probably shouldn't use that. 顺便提一下,不推荐使用模式语法n+1 ,因此您可能不应该使用它。 Instead, do this: 相反,这样做:

(x:xs) `get` n = xs `get` (n - 1)

Just because you put it in parenthesis doesn't make it an infix function. 仅仅因为你把它放在括号中并不会使它成为中缀函数。 Infix functions can only be made via symbols or backticks. 中缀功能只能通过符号或反引号来实现。 See the Haskell report for specifics. 有关详细信息,请参阅Haskell报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM