{- Exercise 3.1 -}
-- Drop as many parentheses in the expressions below as possible.
-- ((f (g (3) 5) (10)) !! ((y - 7) + (5 || x)))
--
-- ((1 >> (2 >>= (f x))) . (y : (a ++ True)))
--
{- Exercise 3.2 -}
-- version where recursion counts downwards until 0
sum_down :: Integer -> Integer
sum_down n = error "TODO"
-- version where recursion counts upwards until n
sum_up :: Integer -> Integer
sum_up n = error "TODO"
-- any solution
fib :: Integer -> Integer
fib n = error "TODO"
{- Exercise 3.3 -}
isDivisible :: Integer -> Integer -> Bool
isDivisible x y = error "TODO, no mod allowed"
isPrime :: Integer -> Bool
isPrime n = error "TODO"
{- Exercise 3.4 -}
-- define the function and add the type signature
-- threeEqual ::
threeEqual x y z = undefined
{- Exercise 3.5 -}
-- Add a question regarding functional programming or the course here:
-- For example:
-- * When is the midterm test?
-- * How do the tests in the template files work?
-- * Am I allowed to submit one of the above example questions as my question? (Answer: No.)
{- Tests -}
{- You don't have to understand the Haskell-code in the tests,
but you can just invoke them after having implemented
some exercises -}
-- tests for sum
sums = [0,1,3,6,10,15,21,28,36,45,55]
test_sum_down = map sum_down [0..10] == sums
|| error "test failed on sum_down"
test_sum_up = map sum_up [0..10] == sums
|| error "test failed on sum_up"
test_sum = test_sum_up && test_sum_down
-- tests for fib
fibs = [0,1,1,2,3,5,8,13,21,34]
test_fib = map fib [0 .. 9] == fibs
|| error "test fib failed"
-- tests for primes
primes = [2,3,5,7,11,13,17,19]
test_primes = filter isPrime [0..20] == primes
|| error "test_primes failed"
testThreeEqual :: Bool
testThreeEqual =
threeEqual 'a' 'a' 'a'
&& not (threeEqual True False True)
&& not (threeEqual 2 1 1)
&& not (threeEqual 1 1 2)
&& not (threeEqual 1 2 2)
&& not (threeEqual 2 1 2)
&& not (threeEqual 2 2 1)
&& not (threeEqual "a" "b" "c")
|| error "test for threeEqual failed"
-- test for all exercises
test_all = test_primes && test_sum && test_fib && testThreeEqual