return
. 대신, return
이하는 일은 모나드에 값을 삽입하는 것입니다 (이 경우 IO
). 당신은 옵션
가장 간단한의 몇
scrapePage :: String -> IO()
scrapePage url = do
doc <- fromUrl url
title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText
if (isNothing title) then return() else do
date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
if (isNothing date) then return() else do
-- etc
-- make page object and send it to db
return()
또 다른 옵션은 IO
모나드가없는 것이 여기 unless
scrapePage url = do
doc <- fromUrl url
title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText
unless (isNothing title) do
date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
unless (isNothing date) do
-- etc
-- make page object and send it to db
return()
일반적인 문제를 사용하는 경우 사용하는 것입니다해야 제어 효과 (예외 제외). 위의 코드 중 어느 것도 없었다 : 당신이 정말 당신이 ContT
scrapePage :: String -> IO()
scrapePage url = runContT return $ do
doc <- fromUrl url
title <- liftM headMay $ runX $ doc >>> css "head.title" >>> getText
when (isNothing title) $ callCC ($())
date <- liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
when (isNothing date) $ callCC ($())
-- etc
-- make page object and send it to db
return()
경고를 사용할 필요가 만발한 제어 효과를 얻으려면 다른 한편으로는, 당신은 아마 모나드 변압기
scrapePage url = liftM (maybe() id) . runMaybeT $ do
doc <- liftIO $ fromUrl url
title <- liftIO $ liftM headMay $ runX $ doc >>> css "head.title" >>> getText
guard (isJust title)
date <- liftIO $ liftM headMay $ runX $ doc >>> css "span.dateTime" ! "data-utc"
guard (isJust date)
-- etc
-- make page object and send it to db
return()
를 사용할 수 있습니다 테스트를 거쳤거나 유형을 확인했습니다!
이게 원하는가요? http://www.haskellforall.com/2012/07/breaking-from-loop.html –