From c6cbe22e33ca6d712cc81a83c00dd011cb187417 Mon Sep 17 00:00:00 2001 From: Sam Halliday Date: Thu, 13 Sep 2018 13:54:12 +0100 Subject: [PATCH] fixup! another edit of Haskell --- manuscript/backmatter.md | 69 +++++++++++++++++++++------------------- manuscript/book.org | 64 +++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 64 deletions(-) diff --git a/manuscript/backmatter.md b/manuscript/backmatter.md index 4dde76a2..a6a7d612 100644 --- a/manuscript/backmatter.md +++ b/manuscript/backmatter.md @@ -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 @@ -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"} ~~~~~~~~ @@ -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"} ~~~~~~~~ @@ -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"} ~~~~~~~~ @@ -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 diff --git a/manuscript/book.org b/manuscript/book.org index 5220b30a..0e44ac18 100644 --- a/manuscript/book.org +++ b/manuscript/book.org @@ -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 @@ -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 @@ -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 =@scalaz.deriving=. 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 @@ -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 @@ -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 =@scalaz.deriving=. 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