why learn new languages? Simple! It gives perspective
I'd been trying to learn haskell for a while, (I have an obsession you see) but it somehow slipped away.
Then, recently I took a coursera course where I had to learn python comprehensions. Yesterday I got back to http://learnyouahaskell.com/ and it was a breeze! How? Learning Python Comprehensions gave me a new perspective, it got me to think of a problem more functionally.
Different programming languages solve problems with different ideas. So I went back to ProjectEuler, this time trying to solve the problems in a number of languages:
https://github.com/gideondsouza/projecteuler-multilingual
Take Project Euler Problem 1 : Find the sum of all the multiples of 3 or 5 below 1000.
In Perl it looks like this
#!/usr/bin/perl
use Modern::Perl;
my $A = 0;
foreach my $x (1..999)
{
$A = $A + $x if ($x % 3 == 0 or $x % 5 == 0);
}
say $A;
With a Python comprehension:
#!/usr/bin/python3
A = sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])
print(A);
And With haskell:
main = print(prob1)
prob1 = sum [x | x <- [1..1000], x `mod` 3 == 0 || x `mod` 5 == 0 ]
So go ye! Learn more languages, even if you won't use it in production, it will give you a new and fresh perspective when writing production code.
Written by Gideon Israel Dsouza
Related protips
9 Responses
I wolud write Haskell code some other way:
mod3 :: Int -> Int
mod3 = reverse mod 3
mod5 :: Int -> Int
mod5 = reverse mod 5
select35 :: [Int] -> [Int]
select35 = select mod5 $ select mod3
main :: IO ()
main = print . sum . select35 [1..1000]
Perl 6:
say [+] grep * %% (3|5), ^1000;
This is beautiful! :) I still don't understand all the syntax, but I will soon :)
wow! Sweet. I've been looking to get into perl6 soon.
totaly agree. maybe after python/haskell you can for for this http://www.rust-lang.org/ . i personally come form java and i love python it just gave me a different point of view. especially when you come from a language that doesn't have closures (yet).
Yeah! I picked up ActionSctipt 3 with little effort after reading through a Java book.
I completely agree with this post, the more languages you know, typically, the better code you write in every language.
Problems like this are great examples of why I love the syntax of Ruby. The solution is incredibly easy and the code is very readable.
(0..999).select{|value| value % 3 == 0 || value % 5 == 0}.inject(0, :+)
# For the more wordy Rubyists:
(0..999).select{|value| value.modulo(3).zero? or value.modulo(5).zero?}.inject(0, :+)
@kazw looks beautiful! And after looking at it now looks immediately understandable. I'd given ruby a crack 2 years ago without too much luck. I'm going to have another go at it soon I think!
A feature that I like in Haskell is the function composition. Understanding how you can apply a function, and then pass the result as the parameter for another function is just awesome. Since I learned this in Haskell I've tried to use it wherever I can, I think that a developer doesn't learn a language, but learns good practices and perspectives as you said.