Yet Another Haskell Tutorial を始めて見ました。

仕事が一段落して余裕ができたので、HaskellProject Euler を解いてみようかと思ったのですが、すっかり忘れてしまっているので改めてTutorialから勉強を始めて見ました。

Yet Another Haskell Tutorial にはExerciseが付いているので、自分の解答を備忘録代わりに書いていこうと思います。

Exercise 3.2 Use a combination of fst and snd to extract the character out of the tuple
((1,’a’),"foo").

-- Answer 3.2
tuple = ((1, 'a'), "foo")
main = print (snd $ fst tuple)

Exercise 3.3 Use map to convert a string into a list of booleans, each element in the
new list representing whether or not the original element was a lower-case character.
That is, it should take the string “aBCde” and return [True,False,False,True,True].

import Char

str = "aBCde"
main = print $ map Char.isLower str

Exercise 3.4 Use the functions mentioned in this section (you will need two of them)
to compute the number of lower-case letters in a string. For instance, on “aBCde” it
should return 3.

import Char

str = "aBCde"
main = print $ length $ filter Char.isLower str

Exercise 3.5 We’ve seen how to calculate sums and products using folding functions.
Given that the function max returns the maximum of two numbers, write a function
using a fold that will return the maximum value in a list (and zero if the list is empty).
So, when applied to [5,10,2,8,1] it will return 10. Assume that the values in the list are
always >= 0. Explain to yourself why it works.

list = [5,10,2,8,1]
main = print $ foldl max 0 list

Exercise 3.6 Write a function that takes a list of pairs of length at least 2 and returns
the first component of the second element in the list. So, when provided with
[(5,’b’),(1,’c’),(6,’a’)], it will return 1.

list = [(5,'b'),(1,'c'),(6,'a')]
main = print $ fst $ head $ tail list