aboutsummaryrefslogtreecommitdiff
path: root/sem7/pp/lec5.hs
blob: 37f6b1228b37d5544597e1833d92fde9b302a53f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Data.Set (Set, empty, member, insert)

-- Opgaver før lecture
sum' x | x > 0 = x + sum' (x-1)
       | otherwise = 0

flip' = map (\(x, y) -> (y, x))
-- Here i would guess that the type is [(a, b)] -> [(b, a)]
-- When quering i get flip' :: [(b, a)] -> [(a, b)]
-- Yeah they are the same


-- I would say the type of fib is (Eq a, Num a, Num b) => a -> b
-- Okay det vil nok være bedre at bruge Integral, eftersom fib tal kun er integer

fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)


-- I would say that the complexity is O(2^n), because we apply
-- fib two times for every invocation of fib.
-- Which does kind of suck

reverse' [] = []
reverse' (x : xs) = reverse' xs ++ [x]


-- Okay so it's clear that we accept lists.
-- However it's a bit unclear which types we accept.
-- Fortunately it does not really matter as do not do anything with x itself.
-- I would therefore way that the type of reverse' :: [a] -> [a].

-- Running `:t` on reverse' reveals that we where correct.

-- Hmm i would say that a ispalindrome function would have type (Eq c) => [c] -> Bool

ispalindrome x = x == reverse' x


-- Okay assuming that a are all integers, i
-- would say that the fype of cfrac :: (Real a, Integral b) => a -> b -> [b]

cfrac _ 0 = []
cfrac x n = let intPart = truncate x in
              intPart : cfrac (1 / (x - fromIntegral intPart)) (n-1)
-- REMEMBER fromInteger for ***'s sake.
-- Without it, x will be tagged with (RealFrac a, Integral a) => a,
-- which destroys everything.

-- Hmm it does not really work perfectly because of the rounding errors


-- Lets just create the last function instead. It must extract the last element of a list.
-- It's type is probably last :: [a] -> maybe a

last' [] = Nothing
last' (x:[]) = Just x
last' (_:xs) = last' xs

-- Flatten a two level deep list.
flatten' [] = []
flatten' (x:xs) = x ++ flatten' xs

-- Okay so we can with curtianty say that the function takes an array of something.
-- This is clear by the pattern matches.
-- However because we use ++ we also know that we return a list, as it's signature is [a] -> [a] -> [a].
-- From this we can also infer that x must be a list.
-- The function type is therefore flatten' :: [[a]] -> [a]

-- When running :t on flatten' we get the same.