aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-10-12 15:39:30 +0200
committerJulian T <julian@jtle.dk>2021-10-12 15:39:30 +0200
commit1a05bcc816d3c956e3ffe85a9fd4abadde54cd3e (patch)
tree31246433493f1d1cbc0fdccd0bde03ee4ef347a3
parent057e8bf56f3aba60cca2ef940cbb73cb5e4678e2 (diff)
Solve some of the haskell exercises
-rw-r--r--sem7/pp/lec5.hs43
1 files changed, 41 insertions, 2 deletions
diff --git a/sem7/pp/lec5.hs b/sem7/pp/lec5.hs
index 246c85a..1b596f5 100644
--- a/sem7/pp/lec5.hs
+++ b/sem7/pp/lec5.hs
@@ -1,9 +1,48 @@
-- Opgaver før lecture
-sum' 0 = 0
-sum' x = x + sum' (x-1)
+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.