lab08: init
This commit is contained in:
29
lab08/stack.hs
Normal file
29
lab08/stack.hs
Normal file
@@ -0,0 +1,29 @@
|
||||
import Control.Monad.State
|
||||
|
||||
type Stack = [Int]
|
||||
|
||||
pop :: Stack -> (Int,Stack)
|
||||
pop (x:xs) = (x,xs)
|
||||
|
||||
push :: Int -> Stack -> ((),Stack)
|
||||
push a xs = ((),a:xs)
|
||||
|
||||
stackManip :: Stack -> (Int, Stack)
|
||||
stackManip stack = let
|
||||
((),newStack1) = push 3 stack
|
||||
(a ,newStack2) = pop newStack1
|
||||
in pop newStack2
|
||||
|
||||
|
||||
pop' :: State Stack Int
|
||||
pop' = State $ \(x:xs) -> (x,xs)
|
||||
|
||||
push' :: Int -> State Stack ()
|
||||
push' a = State $ \xs -> ((),a:xs)
|
||||
|
||||
stackManip' :: State Stack Int
|
||||
stackManip' = do
|
||||
push' 3
|
||||
a <- pop'
|
||||
pop'
|
||||
|
||||
Reference in New Issue
Block a user