Skip to content

Commit

Permalink
rename fold.hs to folding-lists.hs, change to reflect what's going on…
Browse files Browse the repository at this point in the history
… the site
  • Loading branch information
chris-martin committed Jan 24, 2020
1 parent f9b41d5 commit fcb60b4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
36 changes: 0 additions & 36 deletions fold.hs

This file was deleted.

30 changes: 30 additions & 0 deletions folding-lists.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Data.Foldable (foldr, foldMap, fold)
import Prelude hiding (sum)

sum :: [Integer] -> Integer
sum xs = foldr (+) 0 xs

commaList :: [String] -> String
commaList xs = foldr commaSep "" xs
where
commaSep x "" = x
commaSep x phrase = x ++ ", " ++ phrase

bulletList :: [String] -> String
bulletList xs = foldMap bulletItem xs
where
bulletItem x = " - " ++ x ++ "\n"

smashTogether :: [String] -> String
smashTogether xs = fold xs

main =
do
let numbers = enumFromTo 1 5
putStrLn (show numbers)
putStrLn (show (sum numbers))

let words = ["One", "Two", "Three", "Four", "Five"]
putStrLn (commaList words)
putStr (bulletList words)
putStrLn (smashTogether words)
9 changes: 9 additions & 0 deletions outputs/folding-lists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[1,2,3,4,5]
15
One, Two, Three, Four, Five
- One
- Two
- Three
- Four
- Five
OneTwoThreeFourFive
1 change: 1 addition & 0 deletions tools/outputs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ in
(run "dynamic" ../dynamic.hs {})
(run "enum-ranges" ../enum-ranges.hs {})
(run "file-handles" ../file-handles.hs {})
(run "folding-lists" ../folding-lists.hs {})
(run "for-loops" ../for-loops.hs {})
(run "functions" ../functions.hs {})
(run "hashing" ../hashing.hs {})
Expand Down

0 comments on commit fcb60b4

Please sign in to comment.