Created
July 30, 2012 15:14
-
-
Save delbertooo/3207677 to your computer and use it in GitHub Desktop.
callCC Beispiele
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Without callCC | |
square :: Int -> Cont r Int | |
square n = return (n ^ 2) | |
-- With callCC | |
square :: Int -> Cont r Int | |
square n = callCC $ \k -> k (n ^ 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
k :: a -> Cont r b |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vergnuegungsparkbesuch :: Int -> String | |
vergnuegungsparkbesuch budget = (`runCont` id) $ do | |
str <- callCC $ \k -> do | |
when (budget < 20) (k "Ich konnte mir nicht mal den Eintritt leisten.") | |
let s = "So, ich bin im Park.\n" | |
let budget' = budget - 20 | |
attraktion1 <- callCC $ \k' -> do | |
when (budget' <= 0) (k' "Ich habe kein Geld mehr, ich sehe mich nur mal um.\n") | |
when (budget' < 10) (k' "Ich gehe nur was essen, fuer mehr reicht es nicht.\n") | |
when (budget' > 979) $ (k "Ich bin zu reich fuer diese Welt, ich habe keinen Spass.\n") | |
let s' = "Ich bin Autoscooter gefahren.\n" | |
let budget'' = budget' - 5 | |
attraktion2 <- callCC $ \k'' -> do | |
let fahrten = budget'' `div` 5 | |
when (fahrten > 6) (k' "Ich bin ganz oft Achterbahn gefahren. Ploetzlich merkte ich, \ | |
\dass ich den Herd angelassen hatte.\n") | |
return $ "Ich bin " ++ show fahrten ++ "-mal Achterbahn gefahren.\n" | |
return $ s' ++ attraktion2 ++ "Nun habe ich mein Geld verfahren.\n" | |
return $ s ++ attraktion1 ++ "Ich verlasse den Park.\n" | |
return $ "Tagebuch:\n\n" ++ str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment