Skip to content

Commit

Permalink
add for-loops.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-martin committed Jul 24, 2019
1 parent 15afbc3 commit 2722707
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
35 changes: 35 additions & 0 deletions for-loops.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Data.Foldable (for_)
import Data.Traversable (for)

import Control.Monad (when)

main =
do
putStr "Numbers:"
for_ [1..5] $ \i ->
do
putStr " "
putStr (show i)
putStr "\n"

putStr "Odds:"
for_ [1..5] $ \i ->
when (odd i) $
do
putStr " "
putStr (show i)
putStr "\n"

putStr "Odds:"
for_ (filter odd [1..5]) $ \i ->
do
putStr " "
putStr (show i)
putStr "\n"

tens <-
for [1..3] $ \i ->
do
putStr (show i ++ " ")
return (i * 10)
putStr ("(sum: " ++ show (sum tens) ++ ")\n")
4 changes: 4 additions & 0 deletions outputs/for-loops.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Numbers: 1 2 3 4 5
Odds: 1 3 5
Odds: 1 3 5
1 2 3 (sum: 60)
1 change: 1 addition & 0 deletions tools/outputs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ in
(run "common-types.txt" ../common-types.hs)
(run "hashing.txt" ../hashing.hs)
(run "hello-world.txt" ../hello-world.hs)
(run "for-loops.txt" ../for-loops.hs)
(run "threads.txt" ../threads.hs)
(run "variables.txt" ../variables.hs)
]

0 comments on commit 2722707

Please sign in to comment.