Last active
August 29, 2015 14:15
-
-
Save michaelt/7ef6ec1824fb638be203 to your computer and use it in GitHub Desktop.
more tolerant 'do'
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
{-#LANGUAGE RebindableSyntax, RecordWildCards, CPP, OverloadedStrings #-} | |
import Data.Monoid | |
import Prelude | |
import Control.Monad | |
import Data.String | |
import Text.Blaze.Html5 as H | |
import Text.Blaze.Html5.Attributes as A | |
import qualified Text.Blaze as B | |
data Person = Person {name :: String, age :: Int} | |
greet Person{..} = do | |
let (>>) = (<>) | |
"Your name is " | |
name | |
" and your age is " | |
show age | |
greet2 Person{..} = do {"Your name is "; name; " and your age is "; show age} | |
where (>>) = (<>) | |
#define monoid let (>>) = (<>) -- yeck | |
greet3 Person{..} = do monoid | |
"your name is " | |
name | |
" and your age is " | |
show age | |
-- here the point would be to dispense with the Monad instance | |
-- everyone whines about: | |
#define blaze let (>>) = (<>) | |
numbers n = do blaze | |
docTypeHtml $ do | |
H.head $ do | |
H.title "Natural numbers" | |
body $ do | |
p "A list of natural numbers:" | |
ul $ foldMap (li . toHtml) [1 .. n] | |
#define infix(x) let (>>) = (x) | |
aa n = do infix(+) | |
13 | |
n | |
3 * n | |
foldr (*) 1 $ [1..n] ++ [5..6] | |
bb f g x = do infix($) | |
f | |
g | |
x | |
mklist = do infix(:) | |
sum [1..20] | |
product [2..5] | |
foldr (+) 0 [2..4] | |
[] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment