Desugaring'do'块

时间:2015-09-20 19:24:28

标签: haskell syntax monads

我在翻译do声明时遇到了一些麻烦:

ex10 :: [Int]
ex10 = do
    num <- [1..20]
    guard (even num)
    guard (num `mod` 3 == 0)
    return num

采用某种monad语法,如下所示:

ex10' :: [Int]
ex10' = [1..20] >>= (guard . even) >>= (guard . (==0) . (mod 3)) >>= \r -> return r

这不起作用,我不太清楚为什么。我想我可能误解了guard,但不确定以什么方式。

1 个答案:

答案 0 :(得分:4)

<body id="top"> <div style="height: 100%;" id="container"> <div style="height: 100%; width:100%; position:absolute;" id="o-wrapper" class="o-wrapper"> <div class="columnsContainer"> <div class="leftColumn"> <div style=" margin:0px auto; border:0px solid #fff; width:auto;"> <div class="lazy"> <img class="imgstylefirst" src="img/3.jpg" alt="" /> <br/> <div class="creditstyle"> Crop Top, Six Crisp Days. Skirt, Topshop. Clutch, Fendi. Rings, Amarilo and Pushmataaha. Shoes, Boohoo. </div> </div> <div class="lazy"> <img class="imgstyle" src="img/2.jpg" alt="" /> <br/> <div class="creditstyle"> Rings, Amarilo, Pushmataaha and Push&amp;Co. </div> </div> <div class="lazy"> <img class="imgstyle" src="img/4.jpg" alt="" /> <br/> <div class="creditstyle"> Top, Six Crisp Days. Shawl, Unif. Rings, Amarilo. </div> </div> <div class="lazy"> <img class="imgstyle" src="img/5.jpg" alt="" /> <br/> <div class="creditstyle"> Jacket, AZUL by moussy. Earrings, Artelier Mx. Rings, Amarilo. </div> </div> <div class="lazy"> <img class="imgstyle" src="img/6.jpg" alt="" /> <br/> <div class="creditstyle"> Jacket, AZUL by moussy. Earrings, Artelier Mx. Rings, Amarilo. </div> </div> </div> </div> <div class="rightColumn"> <div style="padding:70px 0px 50px 0px; margin: 0px 20px;"> <a href="#">PREVIOUS</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a href="#">ALL</a> &nbsp;&nbsp;|&nbsp;&nbsp; <a href="#">NEXT</a> </div> <div style="margin: 0px 20px;"> <span style="100%" class="newstitle">Y</span> <p style="margin-top:-7px;">Preview</p> <hr style="margin: 15px 0 30px 0;" class="gridnews"> <div class="flipper">ONE</div> <div style="margin:10px 0px 40px 0px; display:block" class="flip"> blablabla </div> <div class="flipper">TWO</div> <div style="margin:10px 0px 80px 0px;" class="flip"> Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/>Photographer <br/>Stylist <br/>Makeup <br/>Hair <br/>Model <br/> </div> <div style="margin-top:20px;" class="flipper">THREE</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">FOUR</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">FIVE</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">SIX</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">SEVEN</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">EIGHT</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">NINE</div> <div style="margin-top:10px;" class="flip"> blablabla </div> <div style="margin-top:20px;" class="flipper">TEN</div> <div style="margin-top:10px;" class="flip"> blablabla </div> </div> </div> </div> </div> </div> </body> 的类型为guard,因此您的第三段

MonadPlus m => Bool -> m ()

收到(guard . (==0) . (mod 3)) 类型的值,这不是您想要的。在()表示法

do

被贬低为类似

的东西
do
  guard (even num)
  guard (num `mod` 3 == 0)

因此您可以将guard (even num) >> guard (num `mod` 3 == 0) 实施为

ex10'