-
Notifications
You must be signed in to change notification settings - Fork 60
Description
Hi:
The book is amazing - but a few changes could make it better.
Minor Content Updates
Location followed by description.
3. Types and Typeclasses
String and [Char] are equivalent, but the interactive session might display them interchangeably. Possibly add link to Type Synonyms section.
ghci> :t "Hello!"
"Hello!" :: String
ghci> :t ['h','e','l','l','o']
['h','e','l','l','o'] :: [Char]
-- Maybe unimportant
ghci> :t head
head :: GHC.Stack.Types.HasCallStack => [a] -> a
9. Input and Output
Replace type signature or mention: IO behaves like Monad.
sequence :: Monad m => [m a] -> m [a]
-- Generic type signature
sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)
I don't think your haiku sucks, but if you feel so, then here's one I wrote 😃.
ghci> :load GetContentHaiku.hs
ghci> main
I'm a lil' teapot,
Touch me not,
I may be hot.
Lift the handle and pick me up,
Gently tilt and pour into a cup,
Sip of the cup and say wassup.
# Thank you very much for Avril Lavigne song suggestion.
Random was not part of System to begin with - needed installation.
-- May no longer be ambiguous:
ghci> random (mkStdGen 0)
-- It assumes (Int, StdGen), and is equivalent to
ghci> random (mkStdGen 100) :: (Int, StdGen)
-- B is Data.ByteString.Lazy
ghci> B.pack [99,97,110]
"can"
{-
- GHCI does not display "Chunks".
- The packed data is displayed as is.
-}
ghci> B.cons 85 $ B.pack [80,81,82,84]
"UPQRT"
11. Functors, Applicative Functors and Monoids
Spell check while describing the first functor law: wee we.
Monoids inherit from Semigroup. mappend can be replaced with <> - later is infix.
{-
- mappend is replaced with <>
- <> defines associative operation
- <> is defined in Semigroup
- <> is an infix operation
-}
class Semigroup a => Monoid a where
mempty :: a
<> :: a -> a -> a
mconcat :: [a] -> a
mconcat :: foldr <> mempty
ghci> "one" <> "two"
"onetwo"
-- Likewise for others as well.
ghci> getProduct $ Product 3 <> mempty
3
instance Monoid Ordering where
mempty = EQ
LT <> _ = LT
EQ <> y = y
GT <> _ = GT
-- Foldable is part of Data
import qualified Data.Foldable as F
-- Maybe unimportant: type signature for foldr and F.foldr is similar.
ghci> :t foldr
foldr :: Foldable t => (a -> b -> b) -> b -> t a -> b
Commutative monoids can be executed in parallel.
It can be mentioned that functions operate on set of data and objects, and they can be thought of as morphisms, functors operate on composition of morphisms and category.
Extra
UNIMPORTANT: in a chess game the columns (also called files) are lettered, so the example can be modified for accuracy.
-- ord and chr are part of Data.Char
type KnightPos = (Char,Int)
class Monad m => MonadPlus m where
mzero :: m a
mplus :: m a -> m a -> m a
guard :: (MonadPlus m) => Bool -> m ()
guard True = return ()
guard False = mzero
moveKnight :: KnightPos -> [KnightPos]
moveKnight (c,r) = do
(c',r') <- [(chr (ord c + 2), r - 1),
(chr (ord c + 2), r + 1),
(chr (ord c - 2), r - 1),
(chr (ord c - 2), r + 1),
(chr (ord c + 1), r - 2),
(chr (ord c + 1), r + 2),
(chr (ord c - 1), r - 2),
(chr (ord c - 1), r + 2)]
guard (c' `elem` ['A'..'H'] && r' `elem` [1..8])
return (c',r')
-- Thank you for reminding of the movies.
To Reproduce
ghci
# Program and library.
ghc program.hs
cabal build
Expected behavior
Types and definitions should match.
Desktop
The Glorious Glasgow Haskell Compilation System, version 9.6.7
Additional context
Not all updates are corrections.