Add remaining haskell book exercises
This commit is contained in:
40
Haskell-book/26/Embedded/Embedded.cabal
Normal file
40
Haskell-book/26/Embedded/Embedded.cabal
Normal file
@@ -0,0 +1,40 @@
|
||||
-- This file has been generated from package.yaml by hpack version 0.20.0.
|
||||
--
|
||||
-- see: https://github.com/sol/hpack
|
||||
--
|
||||
-- hash: 4aaf9c578000b87231817edc542c283c58e2e1562cc290df37e9e8a9628885b0
|
||||
|
||||
name: Embedded
|
||||
version: 0.1.0.0
|
||||
author: Eugen Wissner
|
||||
maintainer: belka@caraus.de
|
||||
copyright: 2018 Eugen Wissner
|
||||
license: BSD3
|
||||
build-type: Simple
|
||||
cabal-version: >= 1.10
|
||||
|
||||
library
|
||||
hs-source-dirs:
|
||||
src
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, transformers
|
||||
exposed-modules:
|
||||
OuterInner
|
||||
other-modules:
|
||||
Paths_Embedded
|
||||
default-language: Haskell2010
|
||||
|
||||
test-suite Embedded-test
|
||||
type: exitcode-stdio-1.0
|
||||
main-is: Spec.hs
|
||||
hs-source-dirs:
|
||||
test
|
||||
ghc-options: -threaded -rtsopts -with-rtsopts=-N
|
||||
build-depends:
|
||||
Embedded
|
||||
, base >=4.7 && <5
|
||||
, transformers
|
||||
other-modules:
|
||||
Paths_Embedded
|
||||
default-language: Haskell2010
|
||||
2
Haskell-book/26/Embedded/Setup.hs
Normal file
2
Haskell-book/26/Embedded/Setup.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
import Distribution.Simple
|
||||
main = defaultMain
|
||||
24
Haskell-book/26/Embedded/package.yaml
Normal file
24
Haskell-book/26/Embedded/package.yaml
Normal file
@@ -0,0 +1,24 @@
|
||||
name: Embedded
|
||||
version: 0.1.0.0
|
||||
license: BSD3
|
||||
author: "Eugen Wissner"
|
||||
maintainer: "belka@caraus.de"
|
||||
copyright: "2018 Eugen Wissner"
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- transformers
|
||||
|
||||
library:
|
||||
source-dirs: src
|
||||
|
||||
tests:
|
||||
Embedded-test:
|
||||
main: Spec.hs
|
||||
source-dirs: test
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- Embedded
|
||||
30
Haskell-book/26/Embedded/src/OuterInner.hs
Normal file
30
Haskell-book/26/Embedded/src/OuterInner.hs
Normal file
@@ -0,0 +1,30 @@
|
||||
module OuterInner where
|
||||
|
||||
import Control.Monad.Trans.Except
|
||||
import Control.Monad.Trans.Maybe
|
||||
import Control.Monad.Trans.Reader
|
||||
|
||||
-- We only need to use return once
|
||||
-- because it's one big Monad
|
||||
embedded :: MaybeT
|
||||
(ExceptT String
|
||||
(ReaderT () IO))
|
||||
Int
|
||||
--embedded = return 1
|
||||
embedded = MaybeT . ExceptT . ReaderT $ return . (const (Right (Just 1)))
|
||||
|
||||
-- We can sort of peel away the layers one by one:
|
||||
maybeUnwrap :: ExceptT String
|
||||
(ReaderT () IO) (Maybe Int)
|
||||
maybeUnwrap = runMaybeT embedded
|
||||
|
||||
-- Next
|
||||
eitherUnwrap :: ReaderT () IO
|
||||
(Either String (Maybe Int))
|
||||
eitherUnwrap = runExceptT maybeUnwrap
|
||||
|
||||
-- Lastly
|
||||
readerUnwrap :: ()
|
||||
-> IO (Either String
|
||||
(Maybe Int))
|
||||
readerUnwrap = runReaderT eitherUnwrap
|
||||
66
Haskell-book/26/Embedded/stack.yaml
Normal file
66
Haskell-book/26/Embedded/stack.yaml
Normal file
@@ -0,0 +1,66 @@
|
||||
# This file was automatically generated by 'stack init'
|
||||
#
|
||||
# Some commonly used options have been documented as comments in this file.
|
||||
# For advanced use and comprehensive documentation of the format, please see:
|
||||
# https://docs.haskellstack.org/en/stable/yaml_configuration/
|
||||
|
||||
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
|
||||
# A snapshot resolver dictates the compiler version and the set of packages
|
||||
# to be used for project dependencies. For example:
|
||||
#
|
||||
# resolver: lts-3.5
|
||||
# resolver: nightly-2015-09-21
|
||||
# resolver: ghc-7.10.2
|
||||
# resolver: ghcjs-0.1.0_ghc-7.10.2
|
||||
# resolver:
|
||||
# name: custom-snapshot
|
||||
# location: "./custom-snapshot.yaml"
|
||||
resolver: lts-11.7
|
||||
|
||||
# User packages to be built.
|
||||
# Various formats can be used as shown in the example below.
|
||||
#
|
||||
# packages:
|
||||
# - some-directory
|
||||
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
|
||||
# - location:
|
||||
# git: https://github.com/commercialhaskell/stack.git
|
||||
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# extra-dep: true
|
||||
# subdirs:
|
||||
# - auto-update
|
||||
# - wai
|
||||
#
|
||||
# A package marked 'extra-dep: true' will only be built if demanded by a
|
||||
# non-dependency (i.e. a user package), and its test suites and benchmarks
|
||||
# will not be run. This is useful for tweaking upstream packages.
|
||||
packages:
|
||||
- .
|
||||
# Dependency packages to be pulled from upstream that are not in the resolver
|
||||
# (e.g., acme-missiles-0.3)
|
||||
# extra-deps: []
|
||||
|
||||
# Override default flag values for local packages and extra-deps
|
||||
# flags: {}
|
||||
|
||||
# Extra package databases containing global packages
|
||||
# extra-package-dbs: []
|
||||
|
||||
# Control whether we use the GHC we find on the path
|
||||
# system-ghc: true
|
||||
#
|
||||
# Require a specific version of stack, using version ranges
|
||||
# require-stack-version: -any # Default
|
||||
# require-stack-version: ">=1.6"
|
||||
#
|
||||
# Override the architecture used by stack, especially useful on Windows
|
||||
# arch: i386
|
||||
# arch: x86_64
|
||||
#
|
||||
# Extra directories used by stack for building
|
||||
# extra-include-dirs: [/path/to/dir]
|
||||
# extra-lib-dirs: [/path/to/dir]
|
||||
#
|
||||
# Allow a newer minor version of GHC than the snapshot specifies
|
||||
# compiler-check: newer-minor
|
||||
2
Haskell-book/26/Embedded/test/Spec.hs
Normal file
2
Haskell-book/26/Embedded/test/Spec.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
main :: IO ()
|
||||
main = putStrLn "Test suite not yet implemented"
|
||||
3
Haskell-book/26/Exercises/.gitignore
vendored
Normal file
3
Haskell-book/26/Exercises/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.stack-work/
|
||||
Exercises.cabal
|
||||
*~
|
||||
2
Haskell-book/26/Exercises/Setup.hs
Normal file
2
Haskell-book/26/Exercises/Setup.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
import Distribution.Simple
|
||||
main = defaultMain
|
||||
61
Haskell-book/26/Exercises/app/Main.hs
Normal file
61
Haskell-book/26/Exercises/app/Main.hs
Normal file
@@ -0,0 +1,61 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Main where
|
||||
|
||||
import Control.Monad.IO.Class
|
||||
import Control.Monad.Trans.Class
|
||||
import Control.Monad.Trans.Reader
|
||||
import Data.IORef
|
||||
import qualified Data.Map as M
|
||||
import Data.Maybe (fromMaybe)
|
||||
import Data.Text.Lazy (Text)
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import System.Environment (getArgs)
|
||||
import Web.Scotty.Trans ( ScottyT(..)
|
||||
, ActionT(..)
|
||||
, scottyT
|
||||
, get
|
||||
, html
|
||||
, param )
|
||||
|
||||
data Config =
|
||||
Config {
|
||||
-- that's one, one click!
|
||||
-- two...two clicks!
|
||||
-- Three BEAUTIFUL clicks! ah ah ahhhh
|
||||
counts :: IORef (M.Map Text Integer)
|
||||
, prefix :: Text
|
||||
}
|
||||
|
||||
type Scotty = ScottyT Text (ReaderT Config IO)
|
||||
|
||||
bumpBoomp :: Text
|
||||
-> M.Map Text Integer
|
||||
-> (M.Map Text Integer, Integer)
|
||||
bumpBoomp k m =
|
||||
let (maybeCount, newMap) = M.insertLookupWithKey (\_ _ oldCount -> oldCount + 1) k 1 m
|
||||
in case maybeCount of
|
||||
Nothing -> (newMap, 1)
|
||||
Just oldCount -> (newMap, oldCount + 1)
|
||||
|
||||
app :: Scotty ()
|
||||
app =
|
||||
get "/:key" $ do
|
||||
unprefixed <- param "key"
|
||||
prefix <- lift $ asks prefix
|
||||
let key' = mappend prefix unprefixed
|
||||
counts <- lift $ asks counts
|
||||
(newMap, newInteger) <- liftIO $ bumpBoomp key' <$> readIORef counts
|
||||
liftIO $ writeIORef counts newMap
|
||||
html $ mconcat [ "<h1>Success! Count was: "
|
||||
, TL.pack $ show (newInteger :: Integer)
|
||||
, "</h1>"
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
[prefixArg] <- getArgs
|
||||
counter <- newIORef M.empty
|
||||
let config = Config {counts = counter, prefix = TL.pack prefixArg}
|
||||
runR = flip runReaderT config
|
||||
scottyT 3000 runR app
|
||||
37
Haskell-book/26/Exercises/package.yaml
Normal file
37
Haskell-book/26/Exercises/package.yaml
Normal file
@@ -0,0 +1,37 @@
|
||||
name: Exercises
|
||||
version: 0.1.0.0
|
||||
license: BSD3
|
||||
author: "Eugen Wissner"
|
||||
maintainer: "belka@caraus.de"
|
||||
copyright: "2018 Eugen Wissner"
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- containers
|
||||
- scotty
|
||||
- transformers
|
||||
- text
|
||||
|
||||
library:
|
||||
source-dirs: src
|
||||
|
||||
executables:
|
||||
HitCounter:
|
||||
main: Main.hs
|
||||
source-dirs: app
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
|
||||
tests:
|
||||
Exercises-test:
|
||||
main: Spec.hs
|
||||
source-dirs: test
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- Exercises
|
||||
- hspec
|
||||
75
Haskell-book/26/Exercises/src/Exercises.hs
Normal file
75
Haskell-book/26/Exercises/src/Exercises.hs
Normal file
@@ -0,0 +1,75 @@
|
||||
module Exercises where
|
||||
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import Control.Monad.Trans.Class (lift)
|
||||
import Control.Monad.Trans.Reader (Reader(..), ReaderT(..), runReader, runReaderT)
|
||||
import Control.Monad.Trans.State (StateT(..))
|
||||
import Data.Functor.Identity (Identity(..))
|
||||
|
||||
-- 1. rDec is a function that should get its argument in the context of
|
||||
-- Reader and return a value decremented by one.
|
||||
--
|
||||
-- Note that “Reader” from transformers is ReaderT of Identity and
|
||||
-- that runReader is a convenience function throwing away the
|
||||
-- meaningless structure for you. Play with runReaderT if you like.
|
||||
rDec :: Num a => Reader a a
|
||||
rDec = ReaderT $ dec
|
||||
where dec :: Num a => a -> Identity a
|
||||
dec = return . (flip (-) 1)
|
||||
|
||||
-- 2. Once you have an rDec that works, make it and any inner lamb-
|
||||
-- das pointfree if that’s not already the case.
|
||||
|
||||
|
||||
-- 3. rShow is show, but in Reader.
|
||||
rShow :: Show a => Reader a String
|
||||
rShow = ReaderT $ toString
|
||||
where toString = return . show
|
||||
|
||||
-- 4. Once you have an rShow that works, make it pointfree.
|
||||
|
||||
|
||||
-- 5. rPrintAndInc will first print the input with a greeting, then return
|
||||
-- the input incremented by one.
|
||||
rPrintAndInc :: (Num a, Show a) => ReaderT a IO a
|
||||
rPrintAndInc = ReaderT print
|
||||
where print x = do
|
||||
liftIO $ putStrLn $ "Hi: " ++ show x
|
||||
return $ x + 1
|
||||
|
||||
-- Prelude> runReaderT rPrintAndInc 1
|
||||
-- Hi: 1
|
||||
-- 2
|
||||
-- Prelude> traverse (runReaderT rPrintAndInc) [1..10]
|
||||
-- Hi: 1
|
||||
-- Hi: 2
|
||||
-- Hi: 3
|
||||
-- Hi: 4
|
||||
-- Hi: 5
|
||||
-- Hi: 6
|
||||
-- Hi: 7
|
||||
-- Hi: 8
|
||||
-- Hi: 9
|
||||
-- Hi: 10
|
||||
-- [2,3,4,5,6,7,8,9,10,11]
|
||||
|
||||
|
||||
-- 6. sPrintIncAccum first prints the input with a greeting, then puts
|
||||
-- the incremented input as the new state, and returns the original
|
||||
-- input as a String.
|
||||
sPrintIncAccum :: (Num a, Show a) => StateT a IO String
|
||||
sPrintIncAccum = StateT print
|
||||
where print x = do
|
||||
liftIO $ putStrLn $ "Hi: " ++ show x
|
||||
return $ (show x, x + 1)
|
||||
|
||||
-- Prelude> runStateT sPrintIncAccum 10
|
||||
-- Hi: 10
|
||||
-- ("10",11)
|
||||
-- Prelude> mapM (runStateT sPrintIncAccum) [1..5]
|
||||
-- Hi: 1
|
||||
-- Hi: 2
|
||||
-- Hi: 3
|
||||
-- Hi: 4
|
||||
-- Hi: 5
|
||||
-- [("1",2),("2",3),("3",4),("4",5),("5",6)]
|
||||
24
Haskell-book/26/Exercises/src/Fix.hs
Normal file
24
Haskell-book/26/Exercises/src/Fix.hs
Normal file
@@ -0,0 +1,24 @@
|
||||
module Fix where
|
||||
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
import Control.Monad.Trans.Maybe
|
||||
import Control.Monad
|
||||
|
||||
isValid :: String -> Bool
|
||||
isValid v = '!' `elem` v
|
||||
|
||||
maybeExcite :: MaybeT IO String
|
||||
maybeExcite = do
|
||||
v <- liftIO getLine
|
||||
guard $ isValid v
|
||||
return v
|
||||
|
||||
doExcite :: IO ()
|
||||
doExcite = do
|
||||
putStrLn "say something excite!"
|
||||
excite <- runMaybeT maybeExcite
|
||||
case excite of
|
||||
Nothing -> putStrLn "MOAR EXCITE"
|
||||
Just e ->
|
||||
putStrLn
|
||||
("Good, was very excite: " ++ e)
|
||||
65
Haskell-book/26/Exercises/stack.yaml
Normal file
65
Haskell-book/26/Exercises/stack.yaml
Normal file
@@ -0,0 +1,65 @@
|
||||
# This file was automatically generated by 'stack init'
|
||||
#
|
||||
# Some commonly used options have been documented as comments in this file.
|
||||
# For advanced use and comprehensive documentation of the format, please see:
|
||||
# https://docs.haskellstack.org/en/stable/yaml_configuration/
|
||||
|
||||
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
|
||||
# A snapshot resolver dictates the compiler version and the set of packages
|
||||
# to be used for project dependencies. For example:
|
||||
#
|
||||
# resolver: lts-3.5
|
||||
# resolver: nightly-2015-09-21
|
||||
# resolver: ghc-7.10.2
|
||||
# resolver: ghcjs-0.1.0_ghc-7.10.2
|
||||
#
|
||||
# The location of a snapshot can be provided as a file or url. Stack assumes
|
||||
# a snapshot provided as a file might change, whereas a url resource does not.
|
||||
#
|
||||
# resolver: ./custom-snapshot.yaml
|
||||
# resolver: https://example.com/snapshots/2018-01-01.yaml
|
||||
resolver: lts-11.8
|
||||
|
||||
# User packages to be built.
|
||||
# Various formats can be used as shown in the example below.
|
||||
#
|
||||
# packages:
|
||||
# - some-directory
|
||||
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
|
||||
# - location:
|
||||
# git: https://github.com/commercialhaskell/stack.git
|
||||
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# subdirs:
|
||||
# - auto-update
|
||||
# - wai
|
||||
packages:
|
||||
- .
|
||||
# Dependency packages to be pulled from upstream that are not in the resolver
|
||||
# using the same syntax as the packages field.
|
||||
# (e.g., acme-missiles-0.3)
|
||||
# extra-deps: []
|
||||
|
||||
# Override default flag values for local packages and extra-deps
|
||||
# flags: {}
|
||||
|
||||
# Extra package databases containing global packages
|
||||
# extra-package-dbs: []
|
||||
|
||||
# Control whether we use the GHC we find on the path
|
||||
# system-ghc: true
|
||||
#
|
||||
# Require a specific version of stack, using version ranges
|
||||
# require-stack-version: -any # Default
|
||||
# require-stack-version: ">=1.7"
|
||||
#
|
||||
# Override the architecture used by stack, especially useful on Windows
|
||||
# arch: i386
|
||||
# arch: x86_64
|
||||
#
|
||||
# Extra directories used by stack for building
|
||||
# extra-include-dirs: [/path/to/dir]
|
||||
# extra-lib-dirs: [/path/to/dir]
|
||||
#
|
||||
# Allow a newer minor version of GHC than the snapshot specifies
|
||||
# compiler-check: newer-minor
|
||||
19
Haskell-book/26/Exercises/test/Spec.hs
Normal file
19
Haskell-book/26/Exercises/test/Spec.hs
Normal file
@@ -0,0 +1,19 @@
|
||||
import Control.Monad.Trans.Reader
|
||||
import Exercises
|
||||
import Test.Hspec
|
||||
|
||||
main :: IO ()
|
||||
main = hspec $ do
|
||||
describe "rDec" $ do
|
||||
it "returns a value decremented by one" $ do
|
||||
runReader rDec 1 `shouldBe` 0
|
||||
|
||||
it "decrements all elements of a list" $ do
|
||||
(fmap (runReader rDec) [1..10]) `shouldBe` [0,1,2,3,4,5,6,7,8,9]
|
||||
|
||||
describe "rShow" $ do
|
||||
it "shows a number" $ do
|
||||
runReader rShow 1 `shouldBe` "1"
|
||||
|
||||
it "shows a list" $ do
|
||||
(fmap (runReader rShow) [1..10]) `shouldBe` ["1","2","3","4","5","6","7","8","9","10"]
|
||||
3
Haskell-book/26/MaybeT/.gitignore
vendored
Normal file
3
Haskell-book/26/MaybeT/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.stack-work/
|
||||
MaybeT.cabal
|
||||
*~
|
||||
2
Haskell-book/26/MaybeT/Setup.hs
Normal file
2
Haskell-book/26/MaybeT/Setup.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
import Distribution.Simple
|
||||
main = defaultMain
|
||||
23
Haskell-book/26/MaybeT/package.yaml
Normal file
23
Haskell-book/26/MaybeT/package.yaml
Normal file
@@ -0,0 +1,23 @@
|
||||
name: MaybeT
|
||||
version: 0.1.0.0
|
||||
license: BSD3
|
||||
author: "Eugen Wissner"
|
||||
maintainer: "belka@caraus.de"
|
||||
copyright: "2018 Eugen Wissner"
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
|
||||
library:
|
||||
source-dirs: src
|
||||
|
||||
tests:
|
||||
MaybeT-test:
|
||||
main: Spec.hs
|
||||
source-dirs: test
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
dependencies:
|
||||
- MaybeT
|
||||
56
Haskell-book/26/MaybeT/src/Either.hs
Normal file
56
Haskell-book/26/MaybeT/src/Either.hs
Normal file
@@ -0,0 +1,56 @@
|
||||
module Either where
|
||||
|
||||
import Control.Monad (liftM)
|
||||
import MonadTrans
|
||||
import MonadIO
|
||||
|
||||
newtype EitherT e m a =
|
||||
EitherT { runEitherT :: m (Either e a) }
|
||||
|
||||
-- 1
|
||||
instance Functor m => Functor (EitherT e m) where
|
||||
fmap f (EitherT x) = EitherT $ (fmap . fmap) f x
|
||||
|
||||
-- 2
|
||||
instance Applicative m => Applicative (EitherT e m) where
|
||||
pure x = EitherT $ pure $ pure x
|
||||
|
||||
(EitherT f) <*> (EitherT a) = EitherT $ (<*>) <$> f <*> a
|
||||
|
||||
-- 3
|
||||
instance Monad m => Monad (EitherT e m) where
|
||||
return = pure
|
||||
|
||||
(EitherT em) >>= f = EitherT $ do
|
||||
v <- em
|
||||
case v of
|
||||
Left y -> return $ Left y
|
||||
Right y -> runEitherT (f y)
|
||||
|
||||
|
||||
-- 4
|
||||
-- transformer version of swapEither.
|
||||
-- Hint: write swapEither first, then swapEitherT in terms of the former.
|
||||
swapEither :: Either e a -> Either a e
|
||||
swapEither (Left x) = Right x
|
||||
swapEither (Right y) = Left y
|
||||
|
||||
swapEitherT :: (Functor m)
|
||||
=> EitherT e m a
|
||||
-> EitherT a m e
|
||||
swapEitherT (EitherT x) = EitherT $ fmap swapEither x
|
||||
|
||||
-- 5. Write the transformer variant of the either catamorphism.
|
||||
eitherT :: Monad m
|
||||
=> (a -> m c)
|
||||
-> (b -> m c)
|
||||
-> EitherT a m b
|
||||
-> m c
|
||||
eitherT f g (EitherT x) = x >>= (either f g)
|
||||
|
||||
instance MonadTrans (EitherT e) where
|
||||
lift = EitherT . liftM Right
|
||||
|
||||
instance (MonadIO m)
|
||||
=> MonadIO (EitherT e m) where
|
||||
liftIO = lift . liftIO
|
||||
43
Haskell-book/26/MaybeT/src/Identity.hs
Normal file
43
Haskell-book/26/MaybeT/src/Identity.hs
Normal file
@@ -0,0 +1,43 @@
|
||||
module Identity where
|
||||
|
||||
import MonadIO
|
||||
import MonadTrans
|
||||
|
||||
newtype Identity a =
|
||||
Identity { runIdentity :: a }
|
||||
deriving (Eq, Show)
|
||||
|
||||
instance Functor Identity where
|
||||
fmap f (Identity a) = Identity (f a)
|
||||
|
||||
instance Applicative Identity where
|
||||
pure = Identity
|
||||
(Identity f) <*> (Identity a) = Identity (f a)
|
||||
|
||||
newtype IdentityT f a =
|
||||
IdentityT { runIdentityT :: f a }
|
||||
deriving (Eq, Show)
|
||||
|
||||
instance (Functor m)
|
||||
=> Functor (IdentityT m) where
|
||||
fmap f (IdentityT fa) = IdentityT (fmap f fa)
|
||||
|
||||
instance (Applicative m)
|
||||
=> Applicative (IdentityT m) where
|
||||
pure x = IdentityT (pure x)
|
||||
|
||||
(IdentityT fab) <*> (IdentityT fa) =
|
||||
IdentityT (fab <*> fa)
|
||||
|
||||
instance (Monad m)
|
||||
=> Monad (IdentityT m) where
|
||||
return = pure
|
||||
|
||||
(IdentityT ma) >>= f = IdentityT $ ma >>= runIdentityT . f
|
||||
|
||||
instance (MonadIO m)
|
||||
=> MonadIO (IdentityT m) where
|
||||
liftIO = IdentityT . liftIO
|
||||
|
||||
instance MonadTrans IdentityT where
|
||||
lift = IdentityT
|
||||
40
Haskell-book/26/MaybeT/src/Maybe.hs
Normal file
40
Haskell-book/26/MaybeT/src/Maybe.hs
Normal file
@@ -0,0 +1,40 @@
|
||||
module Maybe where
|
||||
|
||||
import Control.Monad
|
||||
import MonadIO
|
||||
import MonadTrans
|
||||
|
||||
newtype MaybeT m a =
|
||||
MaybeT { runMaybeT :: m (Maybe a) }
|
||||
|
||||
-- compare to the instance for MaybeT
|
||||
instance (Functor m)
|
||||
=> Functor (MaybeT m) where
|
||||
fmap f (MaybeT ma) =
|
||||
MaybeT $ (fmap . fmap) f ma
|
||||
|
||||
instance (Applicative m)
|
||||
=> Applicative (MaybeT m) where
|
||||
pure x = MaybeT (pure (pure x))
|
||||
|
||||
(MaybeT fab) <*> (MaybeT mma) = MaybeT $ (<*>) <$> fab <*> mma
|
||||
|
||||
instance (Monad m)
|
||||
=> Monad (MaybeT m) where
|
||||
return = pure
|
||||
|
||||
-- (>>=) :: MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b
|
||||
(MaybeT ma) >>= f = MaybeT $ do
|
||||
-- ma :: m (Maybe a)
|
||||
-- v :: Maybe a
|
||||
v <- ma
|
||||
case v of
|
||||
Nothing -> return Nothing
|
||||
Just y -> runMaybeT (f y)
|
||||
|
||||
instance MonadTrans MaybeT where
|
||||
lift = MaybeT . liftM Just
|
||||
|
||||
instance (MonadIO m)
|
||||
=> MonadIO (MaybeT m) where
|
||||
liftIO = lift . liftIO
|
||||
5
Haskell-book/26/MaybeT/src/MonadIO.hs
Normal file
5
Haskell-book/26/MaybeT/src/MonadIO.hs
Normal file
@@ -0,0 +1,5 @@
|
||||
module MonadIO where
|
||||
|
||||
class (Monad m) => MonadIO m where
|
||||
-- | Lift a computation from the 'IO' monad.
|
||||
liftIO :: IO a -> m a
|
||||
7
Haskell-book/26/MaybeT/src/MonadTrans.hs
Normal file
7
Haskell-book/26/MaybeT/src/MonadTrans.hs
Normal file
@@ -0,0 +1,7 @@
|
||||
module MonadTrans where
|
||||
|
||||
class MonadTrans t where
|
||||
-- | Lift a computation from
|
||||
-- the argument monad to
|
||||
-- the constructed monad.
|
||||
lift :: (Monad m) => m a -> t m a
|
||||
35
Haskell-book/26/MaybeT/src/Reader.hs
Normal file
35
Haskell-book/26/MaybeT/src/Reader.hs
Normal file
@@ -0,0 +1,35 @@
|
||||
module Reader where
|
||||
|
||||
import MonadIO
|
||||
import MonadTrans
|
||||
|
||||
newtype ReaderT r m a =
|
||||
ReaderT { runReaderT :: r -> m a }
|
||||
|
||||
instance (Functor m)
|
||||
=> Functor (ReaderT r m) where
|
||||
fmap f (ReaderT rma) =
|
||||
ReaderT $ (fmap . fmap) f rma
|
||||
|
||||
instance (Applicative m)
|
||||
=> Applicative (ReaderT r m) where
|
||||
pure a = ReaderT (pure (pure a))
|
||||
|
||||
(ReaderT fmab) <*> (ReaderT rma) =
|
||||
ReaderT $ (<*>) <$> fmab <*> rma
|
||||
|
||||
instance (Monad m)
|
||||
=> Monad (ReaderT r m) where
|
||||
return = pure
|
||||
|
||||
(ReaderT rma) >>= f =
|
||||
ReaderT $ \r -> do
|
||||
a <- rma r
|
||||
runReaderT (f a) r
|
||||
|
||||
instance MonadTrans (ReaderT r) where
|
||||
lift = ReaderT . const
|
||||
|
||||
instance (MonadIO m)
|
||||
=> MonadIO (ReaderT r m) where
|
||||
liftIO = lift . liftIO
|
||||
41
Haskell-book/26/MaybeT/src/State.hs
Normal file
41
Haskell-book/26/MaybeT/src/State.hs
Normal file
@@ -0,0 +1,41 @@
|
||||
module State where
|
||||
|
||||
import MonadIO
|
||||
import MonadTrans
|
||||
|
||||
newtype StateT s m a =
|
||||
StateT { runStateT :: s -> m (a, s) }
|
||||
|
||||
-- 1
|
||||
instance (Functor m)
|
||||
=> Functor (StateT s m) where
|
||||
fmap f (StateT m) = StateT $ \s -> fmap first $ m s
|
||||
where first = uncurry (\t1 t2 -> ((f t1), t2))
|
||||
|
||||
-- 2
|
||||
-- Links:
|
||||
-- http://stackoverflow.com/questions/18673525/is-it-possible-to-implement-applicative-m-applicative-statet-s-m
|
||||
-- https://github.com/NICTA/course/issues/134
|
||||
instance (Monad m)
|
||||
=> Applicative (StateT s m) where
|
||||
pure x = StateT $ (\s -> pure (x, s))
|
||||
StateT g <*> StateT h = StateT $ \s -> keepFirst <$> g s <*> h s
|
||||
where keepFirst (f, s') (x, _) = (f x, s')
|
||||
|
||||
|
||||
-- 3
|
||||
instance (Monad m)
|
||||
=> Monad (StateT s m) where
|
||||
return = pure
|
||||
|
||||
(StateT sma) >>= f =
|
||||
StateT $ \s -> do
|
||||
a <- sma s
|
||||
runStateT (f $ fst a) s
|
||||
|
||||
instance MonadTrans (StateT s) where
|
||||
lift c = StateT $ \s -> c >>= (\x -> return (x, s))
|
||||
|
||||
instance (MonadIO m)
|
||||
=> MonadIO (StateT s m) where
|
||||
liftIO = lift . liftIO
|
||||
66
Haskell-book/26/MaybeT/stack.yaml
Normal file
66
Haskell-book/26/MaybeT/stack.yaml
Normal file
@@ -0,0 +1,66 @@
|
||||
# This file was automatically generated by 'stack init'
|
||||
#
|
||||
# Some commonly used options have been documented as comments in this file.
|
||||
# For advanced use and comprehensive documentation of the format, please see:
|
||||
# https://docs.haskellstack.org/en/stable/yaml_configuration/
|
||||
|
||||
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
|
||||
# A snapshot resolver dictates the compiler version and the set of packages
|
||||
# to be used for project dependencies. For example:
|
||||
#
|
||||
# resolver: lts-3.5
|
||||
# resolver: nightly-2015-09-21
|
||||
# resolver: ghc-7.10.2
|
||||
# resolver: ghcjs-0.1.0_ghc-7.10.2
|
||||
# resolver:
|
||||
# name: custom-snapshot
|
||||
# location: "./custom-snapshot.yaml"
|
||||
resolver: lts-11.6
|
||||
|
||||
# User packages to be built.
|
||||
# Various formats can be used as shown in the example below.
|
||||
#
|
||||
# packages:
|
||||
# - some-directory
|
||||
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
|
||||
# - location:
|
||||
# git: https://github.com/commercialhaskell/stack.git
|
||||
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# extra-dep: true
|
||||
# subdirs:
|
||||
# - auto-update
|
||||
# - wai
|
||||
#
|
||||
# A package marked 'extra-dep: true' will only be built if demanded by a
|
||||
# non-dependency (i.e. a user package), and its test suites and benchmarks
|
||||
# will not be run. This is useful for tweaking upstream packages.
|
||||
packages:
|
||||
- .
|
||||
# Dependency packages to be pulled from upstream that are not in the resolver
|
||||
# (e.g., acme-missiles-0.3)
|
||||
# extra-deps: []
|
||||
|
||||
# Override default flag values for local packages and extra-deps
|
||||
# flags: {}
|
||||
|
||||
# Extra package databases containing global packages
|
||||
# extra-package-dbs: []
|
||||
|
||||
# Control whether we use the GHC we find on the path
|
||||
# system-ghc: true
|
||||
#
|
||||
# Require a specific version of stack, using version ranges
|
||||
# require-stack-version: -any # Default
|
||||
# require-stack-version: ">=1.6"
|
||||
#
|
||||
# Override the architecture used by stack, especially useful on Windows
|
||||
# arch: i386
|
||||
# arch: x86_64
|
||||
#
|
||||
# Extra directories used by stack for building
|
||||
# extra-include-dirs: [/path/to/dir]
|
||||
# extra-lib-dirs: [/path/to/dir]
|
||||
#
|
||||
# Allow a newer minor version of GHC than the snapshot specifies
|
||||
# compiler-check: newer-minor
|
||||
2
Haskell-book/26/MaybeT/test/Spec.hs
Normal file
2
Haskell-book/26/MaybeT/test/Spec.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
main :: IO ()
|
||||
main = putStrLn "Test suite not yet implemented"
|
||||
3
Haskell-book/26/Morra/.gitignore
vendored
Normal file
3
Haskell-book/26/Morra/.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.stack-work/
|
||||
Morra.cabal
|
||||
*~
|
||||
2
Haskell-book/26/Morra/Setup.hs
Normal file
2
Haskell-book/26/Morra/Setup.hs
Normal file
@@ -0,0 +1,2 @@
|
||||
import Distribution.Simple
|
||||
main = defaultMain
|
||||
69
Haskell-book/26/Morra/app/Main.hs
Normal file
69
Haskell-book/26/Morra/app/Main.hs
Normal file
@@ -0,0 +1,69 @@
|
||||
module Main where
|
||||
|
||||
import Control.Monad
|
||||
import Control.Monad.Trans.State
|
||||
import System.Random
|
||||
import System.Console.ANSI (clearScreen)
|
||||
|
||||
type Score = StateT (Integer, Integer) IO (Integer, Integer)
|
||||
|
||||
yourTurn :: IO (Integer, Integer)
|
||||
yourTurn = do
|
||||
putStrLn "Wie viele Finger zeigen Sie?"
|
||||
shown <- liftM read getLine
|
||||
|
||||
putStrLn "Wie viele Finger wird der Gegner zeigen?"
|
||||
guessed <- liftM read getLine
|
||||
|
||||
clearScreen
|
||||
|
||||
return (shown, guessed)
|
||||
|
||||
|
||||
aiTurn :: IO (Integer, Integer)
|
||||
aiTurn = do
|
||||
gen1 <- getStdGen
|
||||
|
||||
let (shown, gen2) = randomR (1, 5) gen1
|
||||
putStrLn $ "Der Gegner zeigt: " ++ (show shown)
|
||||
|
||||
let (guessed, gen3) = randomR (1, 5) gen2
|
||||
putStrLn $ "Der Gegner hat " ++ (show guessed) ++ " geraten."
|
||||
|
||||
setStdGen gen3
|
||||
return (shown, guessed)
|
||||
|
||||
|
||||
score :: IO (Integer, Integer) -> Score
|
||||
score partnerTurn = StateT $ \(s1, s2) -> do
|
||||
you <- yourTurn
|
||||
partner <- partnerTurn
|
||||
let sum = (fst you) + (snd partner)
|
||||
|
||||
let yourScore = if (snd you) == sum then 1 else 0
|
||||
let partnerScore = if (snd partner) == sum then 1 else 0
|
||||
return ((yourScore, partnerScore), (s1 + yourScore, s2 + partnerScore))
|
||||
|
||||
|
||||
loopGame :: IO (Integer, Integer)
|
||||
-> (Integer, Integer)
|
||||
-> IO (Either (Integer, Integer) (Integer, Integer))
|
||||
loopGame partnerTurn currentScore = do
|
||||
(result, s) <- runStateT (score partnerTurn) currentScore
|
||||
putStrLn $ show $ result
|
||||
|
||||
case s of
|
||||
(16, _) -> return $ Left result
|
||||
(_, 16) -> return $ Right result
|
||||
_ -> loopGame partnerTurn s
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
winner <- loopGame aiTurn (0, 0)
|
||||
|
||||
case winner of
|
||||
Left x -> putStrLn $ "You've won! Score: " ++ (show x)
|
||||
Right x -> putStrLn $ "You've lost! Score: " ++ (show x)
|
||||
|
||||
return ()
|
||||
21
Haskell-book/26/Morra/package.yaml
Normal file
21
Haskell-book/26/Morra/package.yaml
Normal file
@@ -0,0 +1,21 @@
|
||||
name: Morra
|
||||
version: 0.1.0.0
|
||||
license: BSD3
|
||||
author: "Eugen Wissner"
|
||||
maintainer: "belka@caraus.de"
|
||||
copyright: "2018 Eugen Wissner"
|
||||
|
||||
dependencies:
|
||||
- base >= 4.7 && < 5
|
||||
- random
|
||||
- transformers
|
||||
- ansi-terminal
|
||||
|
||||
executables:
|
||||
morra:
|
||||
main: Main.hs
|
||||
source-dirs: app
|
||||
ghc-options:
|
||||
- -threaded
|
||||
- -rtsopts
|
||||
- -with-rtsopts=-N
|
||||
65
Haskell-book/26/Morra/stack.yaml
Normal file
65
Haskell-book/26/Morra/stack.yaml
Normal file
@@ -0,0 +1,65 @@
|
||||
# This file was automatically generated by 'stack init'
|
||||
#
|
||||
# Some commonly used options have been documented as comments in this file.
|
||||
# For advanced use and comprehensive documentation of the format, please see:
|
||||
# https://docs.haskellstack.org/en/stable/yaml_configuration/
|
||||
|
||||
# Resolver to choose a 'specific' stackage snapshot or a compiler version.
|
||||
# A snapshot resolver dictates the compiler version and the set of packages
|
||||
# to be used for project dependencies. For example:
|
||||
#
|
||||
# resolver: lts-3.5
|
||||
# resolver: nightly-2015-09-21
|
||||
# resolver: ghc-7.10.2
|
||||
# resolver: ghcjs-0.1.0_ghc-7.10.2
|
||||
#
|
||||
# The location of a snapshot can be provided as a file or url. Stack assumes
|
||||
# a snapshot provided as a file might change, whereas a url resource does not.
|
||||
#
|
||||
# resolver: ./custom-snapshot.yaml
|
||||
# resolver: https://example.com/snapshots/2018-01-01.yaml
|
||||
resolver: lts-11.9
|
||||
|
||||
# User packages to be built.
|
||||
# Various formats can be used as shown in the example below.
|
||||
#
|
||||
# packages:
|
||||
# - some-directory
|
||||
# - https://example.com/foo/bar/baz-0.0.2.tar.gz
|
||||
# - location:
|
||||
# git: https://github.com/commercialhaskell/stack.git
|
||||
# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
|
||||
# subdirs:
|
||||
# - auto-update
|
||||
# - wai
|
||||
packages:
|
||||
- .
|
||||
# Dependency packages to be pulled from upstream that are not in the resolver
|
||||
# using the same syntax as the packages field.
|
||||
# (e.g., acme-missiles-0.3)
|
||||
# extra-deps: []
|
||||
|
||||
# Override default flag values for local packages and extra-deps
|
||||
# flags: {}
|
||||
|
||||
# Extra package databases containing global packages
|
||||
# extra-package-dbs: []
|
||||
|
||||
# Control whether we use the GHC we find on the path
|
||||
# system-ghc: true
|
||||
#
|
||||
# Require a specific version of stack, using version ranges
|
||||
# require-stack-version: -any # Default
|
||||
# require-stack-version: ">=1.7"
|
||||
#
|
||||
# Override the architecture used by stack, especially useful on Windows
|
||||
# arch: i386
|
||||
# arch: x86_64
|
||||
#
|
||||
# Extra directories used by stack for building
|
||||
# extra-include-dirs: [/path/to/dir]
|
||||
# extra-lib-dirs: [/path/to/dir]
|
||||
#
|
||||
# Allow a newer minor version of GHC than the snapshot specifies
|
||||
# compiler-check: newer-minor
|
||||
Reference in New Issue
Block a user