aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author= <julian@jtle.dk>2021-10-13 08:28:04 +0000
committer= <julian@jtle.dk>2021-10-13 08:28:04 +0000
commita1e7ebc959cb7598844d5fbe35a2c33fc18c1b12 (patch)
tree949d2abf09001a19d8e46b19e264d72d94059708
parent1a05bcc816d3c956e3ffe85a9fd4abadde54cd3e (diff)
Add last assignments for pp lecture 5
-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'