Invalid Haskell 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 download a file containing this code.

 Prelude> :load ch6.hs [1 of 1] Compiling Main ( ch6.hs, interpreted ) ch6.hs:21:0: Invalid type signature Failed, modules loaded: none. 

I am trying to make an infix operator.

+4
source share
2 answers

For starters, you don't need to have parentheses around get . However, the syntax for the whole definition looks a bit. I assume you need something like this:

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

Pay attention to the inverse elements around get to use its infix, which is necessary here because the rules for an alphanumeric identifier are different from operators: Operators are made of characters, are default infixes and write them without arguments or use their prefix, you put them in parentheses. Alphanumeric identifiers are prefixes by default, and surrounding them with reverse windows allow their infix to be used.

You can also use the inverse elements on the left side if you want, but it looks a bit strange:

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

By the way, the syntax of the n+1 template is deprecated, so you probably shouldn't use it. Instead, do the following:

 (x:xs) `get` n = xs `get` (n - 1) 
+12
source

Just because you put it in parentheses does not make it an infix function. Infix functions can only be performed using symbols or backlinks. See the Haskell report for more details.

+2
source

Source: https://habr.com/ru/post/1388551/


All Articles