Created
August 28, 2012 15:36
-
-
Save JamieMason/3499183 to your computer and use it in GitHub Desktop.
Haskell vs JavaScript: find the sum of all odd squares that are smaller than 10000
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
sum . takeWhile (<10000) . filter odd . map (^2) $ [1..] |
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
var sum = 0, | |
i = 0, | |
n = 0; | |
while ((n = Math.pow(++i, 2)) < 1e4) { | |
i % 2 !== 0 && (sum += n); | |
} |
Ah, that makes sense - I was wondering why the JS equivalent looked quite convoluted! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some context, the constraints I put on myself for the above were to write as close to a single expression as possible in JavaScript, since that's what Haskell is able to do using only the language's standard library.
You could extend JavaScript with libraries to achieve some like the below;
But the total code used to create that would be much more.