aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sem7/pp/lec5.hs43
1 files changed, 43 insertions, 0 deletions
diff --git a/sem7/pp/lec5.hs b/sem7/pp/lec5.hs
index 1b596f5..3340346 100644
--- a/sem7/pp/lec5.hs
+++ b/sem7/pp/lec5.hs
@@ -1,3 +1,4 @@
+import Data.Set (Set, empty, member, insert)
-- Opgaver før lecture
sum' x | x > 0 = x + sum' (x-1)
@@ -46,3 +47,45 @@ cfrac x n = let intPart = truncate x in
-- 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.
+
+
+valid :: (Integral a, Ord a) => [(a, b)] -> Bool
+valid = recur empty
+ where recur _ [] = True
+ recur known (pair:rest) =
+ if key `member` known
+ then False
+ else recur (insert key known) rest
+ where key = fst pair
+
+
+-- TODO, case where key is not in is unhandled
+lookup' :: (Integral a, Ord a) => [(a, b)] -> a -> b
+lookup' (pair:rest) key = if (fst pair) == key
+ then (snd pair)
+ else lookup' rest key
+
+-- I dont see how findfun and lookup are not the same
+findfun = lookup'