Hacker News new | past | comments | ask | show | jobs | submit login

The Haskell could use a list comprehension...

    divBy x y = y `mod` x /= 0
    divBy3or5 x = divBy 3 x || divBy 5 x
    euler1 n = sum [x | x <- [0..n], divBy3or5 x]
    
    main = print $ euler1 1000
Personally, I'd switch out lines 2 and 3 to:

    euler1 n = sum [x | x <- [0..n], divBy 3 x, divBy 5 x]
Compared with the Clojure (correct me if I'm wrong):

    (defn euler1 [n] (reduce + (filter (fn [x] (or (div-by 3 x) (div-by 5 x)))) (range n)))
Edit: Note - Clojure code doesn't fit into a HN one-liner.



You could shave off a precious few characters using the function reader syntax:

    (defn euler1 [n] (reduce + (filter #(or (div-by 3 %) (div-by 5 %)))) (range n)))
Or if you're into comprehensions:

    (defn euler1 [n] (reduce + (for [x (range n) :when (or (div-by 3 x) (div-by 5 x))] x)))




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: