
On 12:24 Mon 27 Jun , Roelof Wobben wrote:
Oke, Now figure out how I can find the 3 in the list [1,2,3,4] I was thinking about using tail [1,2,3,4] and after that use last [1,2,3] So something like last [ tail [1,2,3,4] Or : let outcome = tail [1,2,3,4]let outcome2 = last [outcome]
tail returns the list except for the head. So for example the list [1,2,3,4] becomes [2,3,4]. Using last on that would return 4, not 3. However the function init returns the list except for the last item, so using init on [1,2,3,4] would return [1,2,3] and then using last on that would return the 3 you wanted. So for example: ghci> last $ init [1,2,3,4] 3 But Data.List exports a function `find :: (a -> Bool) -> [a] -> Maybe a`, which you can use to find 3. ghci> find (== 3) [1,2,3,4] Just 3 Another way could be using head and dropWhile: ghci> head $ dropWhile (/= 3) [1,2,3,4] 3 -- Mats Rauhala MasseR