I am new to programming and to Haskell. I'm having trouble understanding how to define a function. Let's say I want a function that will return element in position a of a list [b]. For a specific a and [b] I can do this in the interpreter:
Prelude> [2, 3, 5, 6] !! 1
Prelude> 3
But I run into trouble if I try to make a function, either in the interpreter or in a text editor that is then loaded:
Prelude> let getElement a [b] = [b] !! a
Prelude> getElement 1 [2, 3, 5, 6]
***Exception: <interactive>:17:5-27: Non-exhaustive pattern in function getElement
a
calls ofhead
on[b]
. Then you would learn some recursion. – mikeb
instead of[b]
– soulchecklet getElement a b = b !! a
. By[b]
you mean a list of one element which isb
. Read a Haskell tutorial, it will help. – nickietail
you needa
applications of, nothead
. – Rotsor:r <filename>.hs
; the error message you get some input combinations are not covers by the function definition you have provided – jev