Skip to content

Commit 783b15b

Browse files
committed
Add type classes example, update do notation example.
1 parent bfec096 commit 783b15b

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

Main.hs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,32 @@ examples =
246246
]))
247247
, ("do",
248248
("Do Notation",
249-
unlines [ "module DoNotation where"
249+
unlines [ "module Prelude where"
250250
, ""
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"
252258
, ""
253-
, "bindMaybe Nothing _ = Nothing"
254-
, "bindMaybe (Just a) f = f a"
259+
, "data Maybe a = Nothing | Just a"
255260
, ""
256-
, "maybe = { ret: Just, bind: bindMaybe }"
261+
, "instance Prelude.Monad Maybe where"
262+
, " ret = Just"
263+
, " (>>=) Nothing _ = Nothing"
264+
, " (>>=) (Just a) f = f a"
257265
, ""
258266
, "isEven n | n % 2 == 0 = Just {}"
259267
, "isEven _ = Nothing"
260268
, ""
261-
, "evenSum a b = maybe do"
269+
, "evenSum a b = do"
262270
, " n <- a"
263271
, " m <- b"
264272
, " let sum = n + m"
265273
, " isEven sum"
266-
, " return sum"
274+
, " ret sum"
267275
]))
268276
, ("tco",
269277
("Tail-Call Elimination",
@@ -274,6 +282,30 @@ examples =
274282
, ""
275283
, "fact = factHelper 1"
276284
]))
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+
]))
277309
]
278310

279311
page :: Maybe String -> Maybe String -> Maybe Response -> ActionM ()

trypurescript.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ author: Phil Freeman
1313
data-dir: ""
1414

1515
executable trypurescript
16-
build-depends: base ==4.*, scotty -any, purescript ==0.2.10.1, containers -any,
16+
build-depends: base ==4.*, scotty -any, purescript ==0.2.15, containers -any,
1717
mtl -any, blaze-html -any, cmdtheline -any
1818
main-is: Main.hs
1919
buildable: True

0 commit comments

Comments
 (0)