Skip to content

Commit 597a1ef

Browse files
committed
Add IO examples
1 parent fe26341 commit 597a1ef

File tree

8 files changed

+121
-0
lines changed

8 files changed

+121
-0
lines changed

chapter-29/WhatHappens.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module WhatHappens where
2+
3+
import Control.Concurrent
4+
import Debug.Trace
5+
6+
myData :: IO (MVar Int)
7+
myData = newEmptyMVar
8+
9+
main :: IO ()
10+
main = do
11+
mv <- myData
12+
putMVar mv 0
13+
mv' <- myData
14+
zero <- takeMVar mv'
15+
print zero
16+
17+
18+
blah :: IO String
19+
blah = return "blah"
20+
21+
blah' = trace "outer trace" blah
22+
23+
woot :: IO String
24+
woot = return (trace "inner trace" "woot")
25+
26+
main2 :: IO ()
27+
main2 = do
28+
b <- blah'
29+
putStrLn b
30+
putStrLn b
31+
w <- woot
32+
putStrLn w
33+
putStrLn w

chapter-29/io-chapter/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright Author name here (c) 2017
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Author name here nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

chapter-29/io-chapter/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# io-chapter

chapter-29/io-chapter/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module IORefTrans where
2+
3+
import Control.Monad (replicateM)
4+
import System.Random (randomRIO)
5+
6+
gimmeShelter :: Bool -> IO [Int]
7+
gimmeShelter True = replicateM 10 (randomRIO (0, 10))
8+
gimmeShelter False = pure [0]
9+
10+

chapter-29/io-chapter/app/Main.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Main where
2+
3+
main :: IO ()
4+
main = undefined

chapter-29/io-chapter/app/NestedIO.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module NestedIO where
2+
3+
import Data.Time.Calendar
4+
import Data.Time.Clock
5+
import System.Random
6+
7+
huehue :: IO (Either (IO Int) (IO ()))
8+
huehue = do
9+
t <- getCurrentTime
10+
let (_, _, dayOfMonth) = toGregorian (utctDay t)
11+
case even dayOfMonth of
12+
True -> return $ Left randomIO
13+
False -> return $ Right (putStrLn "no soup for you")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: io-chapter
2+
version: 0.1.0.0
3+
-- synopsis:
4+
-- description:
5+
homepage: https://github.com/githubuser/io-chapter#readme
6+
license: BSD3
7+
license-file: LICENSE
8+
author: Author name here
9+
maintainer: example@example.com
10+
copyright: 2017 Author name here
11+
category: Web
12+
build-type: Simple
13+
extra-source-files: README.md
14+
cabal-version: >=1.10
15+
16+
executable io-chapter-exe
17+
hs-source-dirs: app
18+
main-is: Main.hs
19+
ghc-options: -threaded -rtsopts -with-rtsopts=-N
20+
build-depends: base
21+
, io-chapter
22+
, random
23+
, time
24+
default-language: Haskell2010
25+
26+
source-repository head
27+
type: git
28+
location: https://github.com/githubuser/io-chapter

0 commit comments

Comments
 (0)