Advertisement
Guest User

Haskell Stacks funktionen

a guest
Aug 14th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Stack(Stack,emptyStack,iSE) where
  2.   data Stack a = Leer | Cons a deriving Show
  3.  
  4.   emptyStack = []
  5.  
  6.   iSE:: [a]-> Bool
  7.   iSE [] = True
  8.   iSE _ = False
  9.  
  10.  -- push :: a -> a -> [a]
  11.   push s e= s ++ [e]
  12.  
  13.   top s = s !! (length s -1)
  14.  
  15.   pop :: [a] -> [a]
  16.   pop = init
  17.  
  18.   s= push (push [2.0] 7.0) 3.0
  19.  
  20.   -- module Stack(Stack,emptyStack,iSE) where
  21.   --    data Stack a = Leer | Cons a (Stack a)
  22.  
  23.   diev :: Int -> Int -> Int
  24.   diev a b | a < b = 0
  25.            | otherwise = (diev (a - b ) b) +1
  26.   add :: [Float]->[Float]
  27.   add s | length s > 2 = push (pop (pop s)) ((s !! ((length s) - 2)) + (s !! ((length s) -1)))
  28.         | length s == 2 = push emptyStack ((s !! ((length s) - 2)) + (s !! ((length s) -1)))
  29.         | otherwise = error"2249"
  30.  
  31.   sub :: [Float]->[Float]
  32.   sub s | length s > 2 = push (pop (pop s)) ((s !! ((length s) - 2)) - (s !! ((length s) -1)))
  33.         | length s == 2 = push emptyStack ((s !! ((length s) - 2)) - (s !! ((length s) -1)))
  34.         | otherwise = error"2253"
  35.  
  36.   mul :: [Float]->[Float]
  37.   mul s | length s > 2 = push (pop (pop s)) ((s !! ((length s) - 2)) * (s !! ((length s) -1)))
  38.         | length s == 2 = push emptyStack ((s !! ((length s) - 2)) * (s !! ((length s) -1)))
  39.         | otherwise = error"2249"
  40.   pdiv :: [Float]->[Float]
  41.   pdiv s | length s > 2 = push (pop (pop s)) ( (s !! ((length s) - 2)) / (s !! ((length s) -1)))
  42.          | length s == 2 = push emptyStack ( (s !! ((length s) - 2)) / (s !! ((length s) -1)))
  43.          | otherwise = error"2249"
  44.   main :: IO()
  45.   main = print(add (add s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement