@@ -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
@@ -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
537−bindMaybe Nothing _ = Nothing
538+instance Monad Maybe where
538−bindMaybe (Just a) f = f a
540−maybe = { ret: Just, bind: bindMaybe }
539+ ret = Just
540+ (>>=) Nothing _ = Nothing
541+ (>>=) (Just a) f = f a
542isEven n | n % 2 == 0 = Just {}
543isEven n | n % 2 == 0 = Just {}
543isEven _ = Nothing
544isEven _ = Nothing
545−evenSum a b = maybe do
546+evenSum a b = do
548 let sum = n + m
549 let sum = n + m
549 isEven sum
550 isEven sum
550− return sum
551+ ret sum
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`.
555This example illustrates the following aspects of `do` notation:
556This example illustrates the following aspects of `do` notation:
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.
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:
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)
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
850+PureScript has basic support for type classes via the `class` and `instance` keywords.
856+instance Show String where
859+instance Show Boolean where
861+ show false = "false"
863+instance (Show a) => Show [a] where
864+ show arr = "[" ++ showArray arr ++ "]"
866+showArray :: forall a. (Show a) => [a] -> String
868+showArray [x] = show x
869+showArray (x:xs) = show x ++ ", " ++ showArray xs