Haskell-Pattern syntax in the context of an expression: _

I study Haskell and write very simple programs. I want to create a function that will return an element to a given position. Here is what I tried to do -

elempos::Int->[a]->a elempos n (b:_)=head (drop n (b:_) ) 

But I get this error when I load the Test.hs file in the GHCi editor.

 Pattern syntax in expression context: _ 

And he says that Failed modules are loaded: none. Since I am very new to the language, I really don’t have a good idea of ​​what the error is (at present, in chapter 4 you will learn that you havekell). Can someone tell me what's wrong here?

+4
source share
1 answer

_ valid only inside templates, you are trying to use it inside an expression: head (drop n (b : _)) . Since you really don't need to decompose the list, and you need a tail, the solution should do:

 elempos n xs = head (drop n xs) 
+11
source

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


All Articles