From aaaabc7d9e6b50e76e166dddcfad61f68fc80ea0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonah=20Br=C3=BCchert?= <jbb@kaidan.im>
Date: Mon, 15 Jan 2024 15:32:07 +0100
Subject: [PATCH] Add solutions

---
 11/Calc.hs   | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 11/Stack.hs  | 68 +++++++++++++++++++++++++++++++++++++++
 11/calcio.hs |  7 +++++
 3 files changed, 164 insertions(+)
 create mode 100644 11/Calc.hs
 create mode 100644 11/Stack.hs
 create mode 100644 11/calcio.hs

diff --git a/11/Calc.hs b/11/Calc.hs
new file mode 100644
index 0000000..878df40
--- /dev/null
+++ b/11/Calc.hs
@@ -0,0 +1,89 @@
+module Calc(upn) where
+
+import Stack
+
+-- calc :: (Num a, Read a) => [ String ] -> Stack a
+-- calc = foldl handle emptyStack
+--     where
+--         handle s "+" = applyToTop2 (+) s
+--         handle s "-" = applyToTop2  (-) s
+--         handle s "*" = applyToTop2 (*) s
+--         handle s str = push ( read str ) s
+--         applyToTop2 f s
+--             | size s < 2 = error " not enough values "
+--             | otherwise = push ( x2 `f ` x1 ) s2
+--             where
+--                 Just (x1 , s1) = pop s
+--                 Just (x2 , s2) = pop s1
+--
+-- valid :: Stack a -> a
+-- valid s
+--     | size s == 1 = result
+--     | isEmpty s = error " empty input "
+--     | otherwise = error " too many values left "
+--     where
+--         (Just result) = peek s
+--
+-- upn :: (Num a, Read a) => String -> a
+-- upn = valid . calc . words
+--
+
+-- calc :: ( Num a , Read a, Integral a) => [ String ] -> Stack a
+-- calc = foldl handle emptyStack
+--     where
+--         handle s "+" = applyToTop2 (+) s
+--         handle s "-" = applyToTop2 (-) s
+--         handle s "*" = applyToTop2 (*) s
+--         handle s "/" = applyToTop2 (div) s
+--         handle s str = push ( read str ) s
+--         applyToTop2 f s
+--             | size s < 2 = error " not enough values "
+--             | otherwise = push ( x2 `f ` x1 ) s2
+--             where
+--                 Just (x1 , s1) = pop s
+--                 Just (x2 , s2) = pop s1
+-- 
+-- valid :: Stack a -> a
+-- valid s
+--     | size s == 1 = result
+--     | isEmpty s = error " empty input "
+--     | otherwise = error " too many values left "
+--     where
+--         (Just result) = peek s
+-- 
+-- upn :: ( Num a , Read a, Integral a) => String -> a
+-- upn = valid . calc . words
+
+
+calc' :: ( Num a , Read a, Floating a) => [ String ] -> Stack a
+calc' = foldl handle emptyStack
+    where
+        handle s "+" = applyToTop2 (+) s
+        handle s "-" = applyToTop2  (-) s
+        handle s "*" = applyToTop2 (*) s
+        handle s "log" = applyToTop1 (logBase 10) s
+        handle s "/" = applyToTop2 (/) s
+        handle s str = push ( read str ) s
+        applyToTop1 :: (a -> a) -> Stack a -> Stack a
+        applyToTop1 f s
+            | size s < 1 = error "no values"
+            | otherwise = push (f x1) s1
+            where
+                Just (x1, s1) = pop s
+        applyToTop2 f s
+            | size s < 2 = error " not enough values "
+            | otherwise = push ( x2 `f ` x1 ) s2
+            where
+                Just ( x1 , s1 ) = pop s
+                Just ( x2 , s2 ) = pop s1
+
+valid' :: Stack a -> a
+valid' s
+    | size s == 1 = result
+    | isEmpty s = error " empty input "
+    | otherwise = error " too many values left "
+    where
+        ( Just result ) = peek s
+
+upn :: ( Num a , Read a, Floating a ) => String -> a
+upn = valid' . calc' . words
diff --git a/11/Stack.hs b/11/Stack.hs
new file mode 100644
index 0000000..af597ba
--- /dev/null
+++ b/11/Stack.hs
@@ -0,0 +1,68 @@
+{--
+Funktionale Programmierung - Wintersemester 2022/2023
+Vorlesung am 04.01.2023
+Katharina Klost
+
+Modul für Stacks
+--}
+
+module Stack(Stack, emptyStack, size, push, pop, peek, isEmpty) where
+  -- Stack Einträge speichern zusätzlich die Größe des Stacks
+  data Stack a = Empty | S Int a (Stack a) deriving Eq
+
+
+  instance (Show a) => Show (Stack a) where
+    show s = '<' : reverse (go s [])
+       where
+         go:: (Show a) => Stack a -> String -> String
+         go Empty acc = ']': acc
+         go (S _ x Empty) acc = ']': ((reverse.show) x) ++ acc
+         go (S _ x stack) acc = go stack $ " ," ++ ((reverse.show) x) ++ acc
+
+  {-
+  Voraussetzung: Keine
+  Ergebnis: Ein leerer Stack ist geliefert
+  -}
+  emptyStack :: Stack a
+  emptyStack = Empty
+
+  {-
+  Voraussetzung: Keine
+  Ergebnis: Die Anzahl der Elemente auf dem Stack ist geliefert
+  -}
+  size :: Stack a -> Int
+  size Empty = 0
+  size (S size _ _) = size
+
+  {-
+  Voraussetzung: Keine
+  Ergebnis: Ein Stack mit x als oberstem Element ist geliefert
+  -}
+  push:: a ->  Stack a -> Stack a
+  push x Empty = S 1 x Empty
+  push x stack@(S size _ _ ) = S (size + 1) x stack
+
+  {-
+  Voraussetzung: Keine
+  Ergebnis: Wenn der Stack leer ist, ist Nothing geliefert, sonst Just ein Tupel aus dem obersten Element
+  und dem restlichen Stack
+  -}
+  pop:: Stack a -> Maybe (a, Stack a)
+  pop Empty = Nothing
+  pop (S _ x s) = Just (x,s)
+
+  {-
+  Voraussetzung: Keine
+  Ergebnis: Wenn der Stack leer ist, ist Nothing geliefert, sonst Just das oberste Element
+  -}
+  peek :: Stack a -> Maybe a
+  peek Empty = Nothing
+  peek (S size x _) = Just x
+
+  {-
+  Voraussetzung: Keine
+  Ergebnis: Wenn der Stack leer ist, ist True geliefert, sonst ist False geliefert
+  -}
+  isEmpty :: Stack a -> Bool
+  isEmpty Empty = True
+  isEmpty _     = False
diff --git a/11/calcio.hs b/11/calcio.hs
new file mode 100644
index 0000000..56b1466
--- /dev/null
+++ b/11/calcio.hs
@@ -0,0 +1,7 @@
+import Calc
+
+main :: IO ()
+main = do
+    line <- getLine
+    let result = upn line
+    print result
-- 
GitLab