Skip to content

Instantly share code, notes, and snippets.

@JustinSDK
Last active August 29, 2015 14:14
Show Gist options
  • Save JustinSDK/797094c6e3b16d510ffe to your computer and use it in GitHub Desktop.
Save JustinSDK/797094c6e3b16d510ffe to your computer and use it in GitHub Desktop.
〈Haskell Tutorial(21)來寫些迴圈吧!〉的解答
while :: Bool -> IO () -> IO ()
while cond value = do
if cond then value
else return ()
forever' :: IO a -> IO ()
forever' value = do
while True $ do
value
forever' value
sequence' :: [IO a] -> IO [a]
sequence' [] = return []
sequence' (x:xs) = do
value <- x
values <- sequence' xs
return (value : values)
mapM' :: (a1 -> IO a) -> [a1] -> IO [a]
mapM' f xs = sequence' $ map f xs
mapM_' :: (a1 -> IO a) -> [a1] -> IO ()
mapM_' f xs = do
mapM' f xs
return ()
forM' :: [a1] -> (a1 -> IO a) -> IO [a]
forM' xs f = sequence' $ map f xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment