git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update README.md
5b2d549Phil Freeman
README.md+32−10
@@ -21,6 +21,7 @@ Bitcoin donations are gratefully accepted at 14ZhCym28WDuFhocP44tU1dBpCzjX1DvhF.
21- Rank N Types
21- Rank N Types
22- Do Notation
22- Do Notation
23- Optional tail-call elimination
23- Optional tail-call elimination
24+- Type Classes
24
25
25## Try It!
26## Try It!
26
27
@@ -534,39 +535,38 @@ Here is an example, using the maybe monad:
534```haskell
535```haskell
535data Maybe a = Nothing | Just a
536data Maybe a = Nothing | Just a
536
537
537−bindMaybe Nothing _ = Nothing
538+instance Monad Maybe where
538−bindMaybe (Just a) f = f a
539−
540−maybe = { ret: Just, bind: bindMaybe }
539+ ret = Just
540+ (>>=) Nothing _ = Nothing
541+ (>>=) (Just a) f = f a
541
542
542isEven n | n % 2 == 0 = Just {}
543isEven n | n % 2 == 0 = Just {}
543isEven _ = Nothing
544isEven _ = Nothing
544
545
545−evenSum a b = maybe do
546+evenSum a b = do
546 n <- a
547 n <- a
547 m <- b
548 m <- b
548 let sum = n + m
549 let sum = n + m
549 isEven sum
550 isEven sum
550− return sum
551+ ret sum
551```
552```
552
553
553`isEven` adds two values of type `Maybe Number` and returns their sum, if the sum is even. If the sum is odd, `evenSum` returns `Nothing`.
554`isEven` adds two values of type `Maybe Number` and returns their sum, if the sum is even. If the sum is odd, `evenSum` returns `Nothing`.
554
555
555This example illustrates the following aspects of `do` notation:
556This example illustrates the following aspects of `do` notation:
556
557
557−- The `do` keyword is preceded by a value `m` which is a record. The record must contain fields `ret` and `bind` which determine the monad which will be used in the computation.
558+- The corresponding type constructor must be an instance of the `Prelude.Monad` type class, which defines the `ret` and `>>=` functions.
558- Statements can have the following form:
559- Statements can have the following form:
559 - `a <- x` which desugars to `m.bind x (\a -> ...)`
560 - `a <- x` which desugars to `m.bind x (\a -> ...)`
560 - `let a = x` which desugars to `(\a -> ...)(x)`
561 - `let a = x` which desugars to `(\a -> ...)(x)`
561− - `return a` which desugars to `m.ret a`
562 - `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
562 - `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
563
563
564Not illustrated here, but equally valid is the use of a binder on the left hand side of `<-` or `=`. For example:
564Not illustrated here, but equally valid is the use of a binder on the left hand side of `<-` or `=`. For example:
565
565
566```haskell
566```haskell
567−test arr = maybe do
567+test arr = do
568 (x:y:_) <- arr
568 (x:y:_) <- arr
569− return x + y
569+ ret (x + y)
570```
570```
571
571
572A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
572A pattern match failure will generate a runtime exception, just as in the case of a regular `case` statement.
@@ -845,4 +845,26 @@ To alias a name of a field defined on a Javascript type to a PureScript function
845foreign import member "length" length :: forall a. [a] -> Number
845foreign import member "length" length :: forall a. [a] -> Number
846```
846```
847
847
848+## Type Classes
849+
850+PureScript has basic support for type classes via the `class` and `instance` keywords.
851+
852+```haskell
853+class Show a where
854+ show :: a -> String
855+
856+instance Show String where
857+ show s = s
858+
859+instance Show Boolean where
860+ show true = "true"
861+ show false = "false"
848
862
863+instance (Show a) => Show [a] where
864+ show arr = "[" ++ showArray arr ++ "]"
865+
866+showArray :: forall a. (Show a) => [a] -> String
867+showArray [] = ""
868+showArray [x] = show x
869+showArray (x:xs) = show x ++ ", " ++ showArray xs
870+```