Skip to content
Snippets Groups Projects
Verified Commit aaaabc7d authored by jonahbeneb02's avatar jonahbeneb02
Browse files

Add solutions

parent 0c186000
No related branches found
No related tags found
No related merge requests found
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
{--
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
import Calc
main :: IO ()
main = do
line <- getLine
let result = upn line
print result
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment