@@ -246,24 +246,32 @@ examples =
246
246
]))
247
247
, (" do" ,
248
248
(" Do Notation" ,
249
- unlines [ " module DoNotation where"
249
+ unlines [ " module Prelude where"
250
250
, " "
251
- , " data Maybe a = Nothing | Just a"
251
+ , " class Monad m where"
252
+ , " ret :: forall a. a -> m a"
253
+ , " (>>=) :: forall a b. m a -> (a -> m b) -> m b"
254
+ , " "
255
+ , " module DoNotation where"
256
+ , " "
257
+ , " import Prelude"
252
258
, " "
253
- , " bindMaybe Nothing _ = Nothing"
254
- , " bindMaybe (Just a) f = f a"
259
+ , " data Maybe a = Nothing | Just a"
255
260
, " "
256
- , " maybe = { ret: Just, bind: bindMaybe }"
261
+ , " instance Prelude.Monad Maybe where"
262
+ , " ret = Just"
263
+ , " (>>=) Nothing _ = Nothing"
264
+ , " (>>=) (Just a) f = f a"
257
265
, " "
258
266
, " isEven n | n % 2 == 0 = Just {}"
259
267
, " isEven _ = Nothing"
260
268
, " "
261
- , " evenSum a b = maybe do"
269
+ , " evenSum a b = do"
262
270
, " n <- a"
263
271
, " m <- b"
264
272
, " let sum = n + m"
265
273
, " isEven sum"
266
- , " return sum"
274
+ , " ret sum"
267
275
]))
268
276
, (" tco" ,
269
277
(" Tail-Call Elimination" ,
@@ -274,6 +282,30 @@ examples =
274
282
, " "
275
283
, " fact = factHelper 1"
276
284
]))
285
+ , (" typeclasses" ,
286
+ (" Type Classes" ,
287
+ unlines [ " module TypeClasses where"
288
+ , " "
289
+ , " class Show a where"
290
+ , " show :: a -> String"
291
+ , " "
292
+ , " instance Show String where"
293
+ , " show s = s"
294
+ , " "
295
+ , " instance Show Boolean where"
296
+ , " show true = \" true\" "
297
+ , " show false = \" false\" "
298
+ , " "
299
+ , " instance (Show a) => Show [a] where"
300
+ , " show arr = \" [\" ++ showArray arr ++ \" ]\" "
301
+ , " "
302
+ , " showArray :: forall a. (Show a) => [a] -> String"
303
+ , " showArray [] = \"\" "
304
+ , " showArray [x] = show x"
305
+ , " showArray (x:xs) = show x ++ \" , \" ++ showArray xs"
306
+ , " "
307
+ , " test = show [true, false]"
308
+ ]))
277
309
]
278
310
279
311
page :: Maybe String -> Maybe String -> Maybe Response -> ActionM ()
0 commit comments