-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename fold.hs to folding-lists.hs, change to reflect what's going on…
… the site
- Loading branch information
1 parent
f9b41d5
commit fcb60b4
Showing
4 changed files
with
40 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters