git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Update README.md
commit5b2d54910022da3b71ee30e79370e3be50c53370 authorPhil Freeman <paf31@cantab.net> dateThu 16 Jan 2014, 22:49 (13 years ago) parentb979b00
1 file changed+32−10
README.md+32−10
README.md+32−10
@@ -21,6 +21,7 @@ Bitcoin donations are gratefully accepted at 14ZhCym28WDuFhocP44tU1dBpCzjX1DvhF.
2121- Rank N Types
2222- Do Notation
2323- Optional tail-call elimination
24+- Type Classes
2425
2526## Try It!
2627
@@ -534,39 +535,38 @@ Here is an example, using the maybe monad:
534535```haskell
535536data Maybe a = Nothing | Just a
536537
537−bindMaybe Nothing _ = Nothing
538−bindMaybe (Just a) f = f a
539−
540−maybe = { ret: Just, bind: bindMaybe }
538+instance Monad Maybe where
539+ ret = Just
540+ (>>=) Nothing _ = Nothing
541+ (>>=) (Just a) f = f a
541542
542543isEven n | n % 2 == 0 = Just {}
543544isEven _ = Nothing
544545
545−evenSum a b = maybe do
546+evenSum a b = do
546547 n <- a
547548 m <- b
548549 let sum = n + m
549550 isEven sum
550− return sum
551+ ret sum
551552```
552553
553554`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`.
554555
555556This example illustrates the following aspects of `do` notation:
556557
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.
558559- Statements can have the following form:
559560 - `a <- x` which desugars to `m.bind x (\a -> ...)`
560561 - `let a = x` which desugars to `(\a -> ...)(x)`
561− - `return a` which desugars to `m.ret a`
562562 - `x` which desugars to `m.bind x (\_ -> ...)` or just `x` if this is the last statement.
563563
564564Not illustrated here, but equally valid is the use of a binder on the left hand side of `<-` or `=`. For example:
565565
566566```haskell
567−test arr = maybe do
567+test arr = do
568568 (x:y:_) <- arr
569− return x + y
569+ ret (x + y)
570570```
571571
572572A 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
845845foreign import member "length" length :: forall a. [a] -> Number
846846```
847847
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"
848862
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+```