Skip to content

Instantly share code, notes, and snippets.

@psfblair
Created August 22, 2010 21:58
Show Gist options
  • Select an option

  • Save psfblair/544328 to your computer and use it in GitHub Desktop.

Select an option

Save psfblair/544328 to your computer and use it in GitHub Desktop.
-- 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