From 245626cb1e2474f95e63ec3b529c29ddd5e529b8 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sun, 16 Jun 2024 14:22:43 +0200 Subject: [PATCH] Solve slide tasks --- Exercises/exercise-4/Slides.hs | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Exercises/exercise-4/Slides.hs diff --git a/Exercises/exercise-4/Slides.hs b/Exercises/exercise-4/Slides.hs new file mode 100644 index 0000000..a845b7a --- /dev/null +++ b/Exercises/exercise-4/Slides.hs @@ -0,0 +1,42 @@ +myMap :: (a -> b) -> [a] -> [b] +-- myMap f [] = [] +-- myMap f (x : xs) = (f x) : (myMap f xs) + +-- inputs: [a, b, c] +-- desired output: [a', b', c'] +-- non end recursive result: +-- myMap f [a, b, c] +-- (f a) : (myMap f [b, c]) +-- (f a) : ((f b) : (myMap f [c])) +-- (f a) : ((f b) : ((f c) : myMap f [])) +-- (f a) : ((f b) : ((f c) : myMap f [])) +-- (f a) : ((f b) : ((f c) : [])) +-- a' : (b' : (c' : [])) +-- [a', b', c'] + +-- ((a' : b') : c') +-- ((f a) : (f b)) : f c + +myMap f = realMap [] + where + realMap acc [] = acc + realMap acc (x : xs) = realMap (acc ++ [f x]) xs + +-- >>> myMap ((+1)) [0, 2, 2, 6] +-- [1,3,3,7] +-- + + +fakTR :: Integer -> Integer +fakTR = fakTR_ 1 + where + fakTR_ :: Integer -> Integer -> Integer + fakTR_ acc 0 = acc + fakTR_ acc n = fakTR_ (n * acc) (n -1) + +fakC :: Integer -> Integer +fakC = fakC_ (const 1) + where + fakC_ f n + | n == 0 = f n + | otherwise = fakC_ (\ x -> n * (f x)) $ n - 1 \ No newline at end of file