Skip to content

Instantly share code, notes, and snippets.

@LeifW
Created June 8, 2011 22:21
Show Gist options
  • Select an option

  • Save LeifW/1015583 to your computer and use it in GitHub Desktop.

Select an option

Save LeifW/1015583 to your computer and use it in GitHub Desktop.
Comparison of languages.
Earlier I said Haskell/Scala/Python/Ruby/CoffeeScript are all pretty much the same.
By that I meant you can write much the same code in all of them, just the syntax might look a bit more weird or arbitrary in some cases.
Of course there are fundamental differences in the type of things you write in static versus dynamic languages, just as there are fundamental differences between what you write in a lazy vs eager language.
Print the numbers one through ten:
Python:
for i in range(1,11):
print(i)
Ruby:
(1..10).each {|i| puts i}
CoffeeScript:
[1..10].forEach (x)=>console.log(x)
Scala:
1 to 10 foreach println
Haskell:
mapM_ print [1..10]
See? Just some syntax futzing, but same damned thing (ok, the idiomatic Python is different, and some don't permit eta reduction, but close 'nuff).
Of course, Haskell also lets you sequence your IO with things like:
foldr ((>>) . print) (return ()) [1..10]
A dumbed-down explanation of that line:
It could also be written:
foldl (\f i-> f >> print i) (return ()) [1..10]
which you could squint and think of as:
foldl( {(f, i)-> f(); print(i) }, noop, [1..10] )
@OscarGodson
Copy link

You forgot JavaScript! How could you forget the greatest language ever! But JS you can do it whatever way you want:

for(x = 1; x < 11; x++){
  console.log(x); 
}

or

var x = 0; 
while(x < 10){
  x++;
  console.log(x); 
}

Or, i guess:

var x = 0;
do {
  x++;
  console.log(x);
}
while (x < 10);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment