Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created October 14, 2009 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jutememo/209766 to your computer and use it in GitHub Desktop.
Save jutememo/209766 to your computer and use it in GitHub Desktop.
module Mainbak03 where
import Stack
type StackOp a b = Stack a -> (b, Stack a)
-- comb :: StackOp a b -> (b -> StackOp a b) -> StackOp a b -- これは間違い
comb :: StackOp a b -> (b -> StackOp a c) -> StackOp a c
comb m n = \stack0 ->
let (x1, stack1) = m stack0
(x2, stack2) = n x1 stack1
in (x2, stack2)
ret :: b -> StackOp a b
ret x = \stack -> (x, stack)
-- comb_ :: StackOp a b -> StackOp a b -> StackOp a b -- これは間違い
comb_ :: StackOp a b -> StackOp a c -> StackOp a c
comb_ m n = m `comb` (\_ -> n)
topis :: (a -> Bool) -> Stack a -> (Bool, Stack a)
topis p s = let (a, s') = pop s
in (p a, s')
main = do
print $ topis (> 4) `comb` (\x1 ->
topis (> 3) `comb` \x2 ->
topis (> 2) `comb` \x3 ->
ret $ and [x1,x2,x3]) $ s
print $ pop `comb` push' $ s
print $ push' 10 `comb_` pop $ s
print $ push' 10 `comb_` push' 9 `comb_` push' 8 $ s
print $ empty' $ s
print $ push' 10 `comb_` empty' `comb_` push' 0 `comb_` push' 1 $ s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment