This book is what I used to learn the programming language Haskell. This page contains all my exercise answers.
Source: Programming in Haskell, by Graham Hutton
Chapter-4
Defining functions
exercise 1
halve :: [Int] -> ([Int], [Int])
halve xs =
(take n xs, drop n xs)
where n = length xs `div` 2
halve :: [Int] -> ([Int], [Int])
halve xs =
splitAt (length xs `div` 2) xsexercise 2
-- a (head & tail)
third :: [a] -> a
third xs = head (tail (tail xs))
-- b (list indexing)
third :: [a] -> a
third xs = xs !! 2
-- c (pattern matching)
third :: [a] -> a
third (_:_:a:_) = aexercise 3
-- a (conditional expression)
safetail :: [a] -> [a]
safetail xs = if length xs > 0 then tail xs else []
-- b (guarded equation)
safetail :: [a] -> [a]
safetail xs | length xs > 0 = tail xs
| otherwise = []
-- c (pattern matching)
safetail :: [a] -> [a]
safetail [] = []
safetail xs = tail xs
-- or:
-- safetail (_:xs) = xsexercise 4
(||) :: Bool -> Bool -> Bool
True || _ = True
_ || True = True
_ = Falseexercise 5
-- Use conditional expressions to define &&.
(<#>) :: Bool -> Bool -> Bool
a <#> b =
if a then
if b then True else False
else
Falseexercise 6
(<#>) :: Bool -> Bool -> Bool
a <#> b =
if a then b else Falseexercise 7
mult :: Int -> Int -> Int -> Int
mult x y z = x*y*z
-- rewritten to use lambda functions.
mult :: Int -> (Int -> (Int -> Int))
mult = \x -> (\y -> (\z -> x * y * z))exercise 8
luhnDouble :: Int -> Int
luhnDouble x = x * 2 `mod` 9
luhn :: Int -> Int -> Int -> Int -> Bool
luhn a b c d =
sum ((map luhnDouble [a,c]) ++ [b,d]) `mod` 10 == 0
--ghci> luhn 1 7 8 4
--True
--ghci> luhn 4 7 8 3
--FalseChapter-5
List comprehensions
- exercise 1
sum [x^2 | x <- [0..100]]
-- 338350exercise 2
grid :: Int -> Int -> [(Int, Int)]
grid n m =
[(x,y) | x <- [0..n], y <- [0..m]]
ghci> grid 1 2
-- [(0,0),(0,1),(0,2),(1,0),(1,1),(1,2)]exercise 3
square :: Int -> [(Int,Int)]
square n =
[(x,y) | (x,y) <- grid n n, x /= y]
ghci> square 2
-- [(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]exercise 4
replicate :: Int -> a -> [a]
replicate n item =
[item | _ <- [1..n]]
ghci> replicate 4 "test"
-- ["test","test","test","test"]exercise 5
isPythagorean :: Int -> Int -> Int -> Bool
isPythagorean x y z =
x^2 + y^2 == z^2
pyths :: Int -> [(Int,Int,Int)]
pyths n =
[(x,y,z) | x <- [1..n], y <- [1..n], z <- [1..n], isPythagorean x y z]
ghci> pyths 10
-- [(3,4,5),(4,3,5),(6,8,10),(8,6,10)]exercise 6
factors :: Int -> [Int]
factors n = [x | x <- [1..n], n `mod` x == 0]
perfects :: Int -> [Int]
perfects limit =
[x | x <- [1..limit], sum (factors x) - x == x]
ghci> perfects 10000
-- [6,28,496,8128]exercise 7
(I did not understand this one)
exercise 8
Use the find library function in Data.List 9.8.2
find :: (a -> Bool) -> [a] -> Maybe a
-- The find function takes a predicate and a list and returns the first element in the list matching the predicate, or Nothing if there is no such element.positions :: Eq a => a -> [a] -> [Int]
positions x xs =
[i | (x',i) <- zip xs [0..], x == x']
-- using find function, though I doubt its correct...
positions :: Eq a => a -> [a] -> [Int]
positions x xs =
[i | (x',i) <- zip xs [0..], isJust (find (==x) [x'])]
positions 2 [1,1,0,2,46,6,8,9,2,3,4,2,4,9,2]
-- [3,8,11,14]
-- You can also use:
positions :: Eq a => a -> [a] -> [Int]
positions x = elemIndices xexercise 9
scalarproduct :: [Int] -> [Int] -> Int
scalarproduct xs ys =
sum [x*y | (x,y) <- zip xs ys]
ghci> scalarproduct [1,2,3] [4,5,6]
-- 32