Created
August 22, 2010 21:58
-
-
Save psfblair/544328 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| -- Alex's simple Haskell version | |
| foldl (+) 0 [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0] | |
| -- List comprehensions, naturally lazy | |
| let multipleOf3Or5 x = (mod x 3 == 0) || (mod x 5 == 0) | |
| foldl (+) 0 $ takeWhile (<1000) [x | x <- [1..], multipleOf3Or5 x] | |
| -- Sets | |
| -- Prelude> :m + Data.Set | |
| let multiplesOf3 = Data.Set.fromList [0,3..1000] | |
| let multiplesOf5 = Data.Set.fromList [0,5..995] | |
| Data.Set.fold (+) 0 $ Data.Set.union multiplesOf3 multiplesOf5 | |
| -- or more briefly: | |
| Data.Set.fold (+) 0 $ Data.Set.union (Data.Set.fromList [0,3..1000]) (Data.Set.fromList [0,5..995]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment