[Haskell-beginners] don't understand this
mats.rauhala at gmail.com
Mon Jun 27 14:58:53 CEST 2011
On 12:47 Mon 27 Jun , Roelof Wobben wrote:
> I must find the k st number in a list.
> So I have this list [1,2,3,4] and I have to find the second number.
> According to the solution the answer is :
> elementAt list i = list !! (i-1)
> But where do I fill in [1,2,3,4] and the 2 ?
If you are unclear about a function, load it up in ghci and check it's
type signature, it can tell you much. The type for elementAt is `[a] ->
Int -> a` which tells you that the first argument is the list and the
second argument is the kth. Actually you can see that also from the
variable names, just replace 'kth' with 'ith'.
The function (!!) is an infix function, meaning that it can be used
between two parameters, but when reading the type signature, it's easier
to think of it as a normal function. The type signature for (!!) is `[a]
-> Int -> a` which means that the first argument is a list and the
second is the index. The functions fetches the item from a list at a
given 0-based index.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 198 bytes
Desc: not available
More information about the Beginners