Skip to content

Commit

Permalink
fixup! another edit of Haskell
Browse files Browse the repository at this point in the history
  • Loading branch information
fommil committed Sep 13, 2018
1 parent aab6f69 commit c6cbe22
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 64 deletions.
69 changes: 36 additions & 33 deletions manuscript/backmatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ keep it surrounded by brackets. The following are equivalent:

{lang="text"}
~~~~~~~~
a `foldLeft` b
foldLeft a b
a `foo` b
foo a b
~~~~~~~~

An infix function can be curried on either the left or the right, often giving
Expand All @@ -273,8 +273,8 @@ parameters much like a Scala `case` clause:
fmap _ Nothing = Nothing
~~~~~~~~

Underscores are a placeholder for ignored parameters and extractors can be in
infix position:
Underscores are a placeholder for ignored parameters and function names can be
in infix position:

{lang="text"}
~~~~~~~~
Expand Down Expand Up @@ -485,33 +485,8 @@ example we can define something similar to Scalaz's `Apply.apply2`
apply2 f fa fb = f <$> fa <*> fb
~~~~~~~~

Note that because of currying, `applyX` is much easier to implement in Haskell
than in Scala. Alternative implementations in comments to demonstrate the
principle:

{lang="text"}
~~~~~~~~
apply3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
apply3 f fa fb fc = f <$> fa <*> fb <*> fc
-- apply3 f fa fb fc = apply2 f fa fb <*> fc
apply4 :: Applicative f => (a -> b -> c -> d -> e) -> f a -> f b -> f c -> f d -> f e
apply4 f fa fb fc fd = f <$> fa <*> fb <*> fc <*> fd
-- apply4 f fa fb fc fd = apply3 f fa fb fc <*> fd
~~~~~~~~

Haskell has typeclass derivation with the `deriving` keyword, the inspiration
for `@scalaz.deriving`. Defining the derivation rules is an advanced topic, but
it is easy to derive a typeclass for an ADT:

{lang="text"}
~~~~~~~~
data List a = Nil | a :. List a
deriving (Eq, Ord)
~~~~~~~~

Since we are talking about `Monad`, it is a good time to introduce `do`
notation, which was the inspiration for Scala's `for` comprehensions:
Since we have introduced `Monad`, it is a good time to introduce `do` notation,
which was the inspiration for Scala's `for` comprehensions:

{lang="text"}
~~~~~~~~
Expand Down Expand Up @@ -546,8 +521,26 @@ where `>>=` is `=<<` with parameters flipped

and `return` is a synonym for `pure`.

Unlike Scala, we do not need to bind unit values with `_ <-`. Non-monadic values
can be bound with the `let` keyword:
Unlike Scala, we do not need to bind unit values, or provide a `yield` if we are
returning `()`. For example

{lang="text"}
~~~~~~~~
for {
_ <- putStr("hello")
_ <- putStr(" world")
} yield ()
~~~~~~~~

translates to

{lang="text"}
~~~~~~~~
do putStr "hello"
putStr " world"
~~~~~~~~

Non-monadic values can be bound with the `let` keyword:

{lang="text"}
~~~~~~~~
Expand All @@ -561,6 +554,16 @@ can be bound with the `let` keyword:
return full
~~~~~~~~

Finally, Haskell has typeclass derivation with the `deriving` keyword, the
inspiration for `@scalaz.deriving`. Defining the derivation rules is an advanced
topic, but it is easy to derive a typeclass for an ADT:

{lang="text"}
~~~~~~~~
data List a = Nil | a :. List a
deriving (Eq, Ord)
~~~~~~~~


## Modules

Expand Down
64 changes: 33 additions & 31 deletions manuscript/book.org
Original file line number Diff line number Diff line change
Expand Up @@ -16670,8 +16670,8 @@ backticks, and an infix function can be called like a regular function if we
keep it surrounded by brackets. The following are equivalent:

#+BEGIN_SRC haskell
a `foldLeft` b
foldLeft a b
a `foo` b
foo a b
#+END_SRC

An infix function can be curried on either the left or the right, often giving
Expand All @@ -16695,8 +16695,8 @@ fmap f (Just a) = Just (f a)
fmap _ Nothing = Nothing
#+END_SRC

Underscores are a placeholder for ignored parameters and extractors can be in
infix position:
Underscores are a placeholder for ignored parameters and function names can be
in infix position:

#+BEGIN_SRC haskell
(<+>) :: Maybe a -> Maybe a -> Maybe a
Expand Down Expand Up @@ -16889,31 +16889,8 @@ apply2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
apply2 f fa fb = f <$> fa <*> fb
#+END_SRC

Note that because of currying, =applyX= is much easier to implement in Haskell
than in Scala. Alternative implementations in comments to demonstrate the
principle:

#+BEGIN_SRC haskell
apply3 :: Applicative f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d
apply3 f fa fb fc = f <$> fa <*> fb <*> fc
-- apply3 f fa fb fc = apply2 f fa fb <*> fc

apply4 :: Applicative f => (a -> b -> c -> d -> e) -> f a -> f b -> f c -> f d -> f e
apply4 f fa fb fc fd = f <$> fa <*> fb <*> fc <*> fd
-- apply4 f fa fb fc fd = apply3 f fa fb fc <*> fd
#+END_SRC

Haskell has typeclass derivation with the =deriving= keyword, the inspiration
for [email protected]=. Defining the derivation rules is an advanced topic, but
it is easy to derive a typeclass for an ADT:

#+BEGIN_SRC haskell
data List a = Nil | a :. List a
deriving (Eq, Ord)
#+END_SRC

Since we are talking about =Monad=, it is a good time to introduce =do=
notation, which was the inspiration for Scala's =for= comprehensions:
Since we have introduced =Monad=, it is a good time to introduce =do= notation,
which was the inspiration for Scala's =for= comprehensions:

#+BEGIN_SRC haskell
do
Expand Down Expand Up @@ -16945,8 +16922,24 @@ flip :: (a -> b -> c) -> b -> a -> c

and =return= is a synonym for =pure=.

Unlike Scala, we do not need to bind unit values with =_ <-=. Non-monadic values
can be bound with the =let= keyword:
Unlike Scala, we do not need to bind unit values, or provide a =yield= if we are
returning =()=. For example

#+BEGIN_SRC scala
for {
_ <- putStr("hello")
_ <- putStr(" world")
} yield ()
#+END_SRC

translates to

#+BEGIN_SRC haskell
do putStr "hello"
putStr " world"
#+END_SRC

Non-monadic values can be bound with the =let= keyword:

#+BEGIN_SRC haskell
nameReturn :: IO String
Expand All @@ -16959,6 +16952,15 @@ nameReturn = do putStr "What is your first name? "
return full
#+END_SRC

Finally, Haskell has typeclass derivation with the =deriving= keyword, the
inspiration for [email protected]=. Defining the derivation rules is an advanced
topic, but it is easy to derive a typeclass for an ADT:

#+BEGIN_SRC haskell
data List a = Nil | a :. List a
deriving (Eq, Ord)
#+END_SRC

*** Modules

Haskell source code is arranged into hierarchical modules with the restriction
Expand Down

0 comments on commit c6cbe22

Please sign in to comment.