Skip to content

Instantly share code, notes, and snippets.

@myuon
Created January 7, 2014 14:03
Show Gist options
  • Save myuon/8299739 to your computer and use it in GitHub Desktop.
Save myuon/8299739 to your computer and use it in GitHub Desktop.
whileを使ったループ(多分分かりやすくはない)
import Control.Monad
while :: (Monad m) => (a -> Bool) -> m a -> m a
while f m = do
a <- m
when (f a) $ while f m >> return ()
return $ a
main = do
putStrLn $ "Can you guess the number I have?"
while (\a -> a /= 42) $ do
a <- getLine
putStrLn $ "You said:" ++ a
return $ read a
putStrLn "That's right!"
Can you guess the number I have?
> 10
You said:10
> 20
You said:20
> 43
You said:43
> 42
You said:42
That's right!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment