Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created October 14, 2009 02:16
Show Gist options
  • Select an option

  • Save jutememo/209726 to your computer and use it in GitHub Desktop.

Select an option

Save jutememo/209726 to your computer and use it in GitHub Desktop.
module Mainbak00 where
import Stack
-- スタックから 2 つの要素を取り出して足し合わせる
sumTwoElem :: Num a => Stack a -> (a, Stack a)
sumTwoElem stack0 = let (x1, stack1) = pop stack0
(x2, stack2) = pop stack1
in (x1+x2, stack2)
-- スタックから 4 つの要素を取り出して足し合わせる
sumFourElem :: Num a => Stack a -> (a, Stack a)
sumFourElem stack0 = let (x1, stack1) = pop stack0
(x2, stack2) = pop stack1
(x3, stack3) = pop stack2
(x4, stack4) = pop stack3
in (x1+x2+x3+x4, stack4)
sumFourElem' :: Num a => (Stack a -> (a, Stack a))
-> Stack a
-> (a, Stack a)
sumFourElem' f = \stack0 ->
let (x1, stack1) = f stack0
(x2, stack2) = f stack1
(x3, stack3) = f stack2
(x4, stack4) = f stack3
in (x1+x2+x3+x4, stack4)
main = do print s
print $ sumTwoElem s
print $ sumFourElem s
print $ sumFourElem' pop $ s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment