
Hello, 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 ? Roelof

On 12:47 Mon 27 Jun , Roelof Wobben wrote:
Hello,
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. -- Mats Rauhala MasseR

Sorry for the doublepost. You should also check out the Learn you a haskell book from http://learnyouahaskell.com which you can read on the internet for free. The first few chapters teach you these exact things you've been asking here. -- Mats Rauhala MasseR

hello, Im reading that book and im now reading chapter4. Where I don't get this : You have this : lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!" So I entered these lines with let for it. So let lucky 7 = "Lucky number seven" let lucky x = "Sorry, you're out of luck, pal!" But if I do lucky 7 or lucky 2 I always get the last sentence. Roelof ---------------------------------------- Date: Mon, 27 Jun 2011 16:00:23 +0300 From: mats.rauhala@gmail.com To: beginners@haskell.org Subject: Re: [Haskell-beginners] don't understand this Sorry for the doublepost. You should also check out the Learn you a haskell book from http://learnyouahaskell.com which you can read on the internet for free. The first few chapters teach you these exact things you've been asking here. -- Mats Rauhala MasseR _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi.
On 27 June 2011 16:33, Roelof Wobben
let lucky 7 = "Lucky number seven" let lucky x = "Sorry, you're out of luck, pal!"
But if I do lucky 7 or lucky 2 I always get the last sentence.
The second let binding shadows the first one. Try: let lucky 7 = "Lucky!"; lucky x = "No luck this time." Or even better, write this in a file and load it into ghci. lucky 7 = "Lucky!" lucky x = "No luck this time." HTH, Ozgur
participants (3)
-
Mats Rauhala
-
Ozgur Akgun
-
Roelof Wobben