Multilingual ProgrammingPython, Haskell, Ruby, Js |
|||
Python |
Haskell |
Ruby |
Js |
Python’s printf
$ cat hello.py
print("Hello")
$ python3 hello.py
Hello
|
Haskell’s printf$ cat hello.hs main = putStrLn "Hello!" $ ghc hello.hs $ ./hello Hello! |
Ruby’s printf$ cat hello.rb puts "hello" $ ruby hello.rb hello |
Js’s printf
$ cat hello.js
console.log("Hello");
$ node hello.js
Hello
|
Python’s mathSimple arithmetic with python 3: $ python3 >>> 1 / 2 0.5 >>> 2 ** 3 8 classic division returns a float: >>> 17 / 3 5.666666666667 >>> 17 // 3 # floor division 5 |
Haskell’s math$ cat math.hs main = do a <- readLn :: IO Integer b <- readLn :: IO Integer putStrLn $ "a + b = " ++ show (a + b) putStrLn $ "a - b = " ++ show (a - b) putStrLn $ "a * b = " ++ show (a * b) $ ghc math.hs $ ./math 32 5 a + b = 37 a - b = 27 a * b = 160 |
Ruby’s math
$ ruby
b = 2 + 3
puts "#{b}"
^D
5
|
Js’s math$ node Welcome to Node.js > 32 / 5 6.4 > 32 / 5 | 0 6 > 12.4 * 2 | 0 24 |
Python’s vars
>>> a = 6
>>> print('a:', a)
a: 6
|
Haskell’s varsv = 3 v :: Int v = 3 let v = 3 |
Ruby’s varsvars with floats:
$ ruby
res = 4.7 / 0.3
puts "#{res}"
^D
15.666666666668
|
Js’s varsvar a = 1; let b = 2; const d = 4; |
Python’s Lists
fruits = ['Banana', 'Apple', 'Lime']
for fruit in fruits:
print('my favorite fruit is:', fruit)
$ python3 list.py my favorite fruit is: Banana my favorite fruit is: Apple my favorite fruit is: Lime |
Haskell’s foreach$ ghci Prelude> ds = map show [1, 2, 3, 4] Prelude> mapM_ putStrLn ds 1 2 3 4 |
Ruby’s Arrays$ cat rb-arr.rb
fruits = ['Banana', 'Apple', 'Lime']
fruit1, fruit2, fruit3 = fruits
puts "#{fruit1}"
puts "#{fruit2}"
puts "#{fruit3}"
$ ruby rb-arr.rb
Banana
Apple
Lime
|
Js’s foreach
var arr = [6, 2, 3, 72, 19];
{
var _num = 0;
for (let _elm of arr) {
_num++;
}
}
|
Python’s Arrays of arrays$ python3 >>> arr = [ ... [1, 2, 3], ... [4, 5, 6], ... [7, 8, 9], ... ] >>> arr[0][1] 2 |
Haskell’s Arrays of arrays |
Ruby’s Arrays of arrays |
Js’s Arrays of arrayslet pr = [ [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 1, 0, 1, 0], [1, 1, 1, 1, 1], [0, 0, 0, 1, 0], ]; |
Python’s switchno switch, use elif |
Haskell’s switch
$ ghci
Prelude> v = 3
Prelude> :{
Prelude| b = case v of
Prelude| 3 -> "Three"
Prelude| _ -> "Not three"
Prelude| :}
Prelude> b
"Three"
|
Ruby’s switch |
Js’s switch
switch (ed) { //
case 37: return 'left' ;
case 38: return 'up' ;
case 39: return 'right';
case 40: return 'down' ;
}
|
Python’s concatsq1 = [1, 4, 9, 16, 25] sq2 = [36, 49, 64, 81, 100] sq3 = sq1 + sq2 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] |
Haskell’s concat$ ghci Prelude> a = [1, 2, 3] Prelude> b = [4, 5, 6] Prelude> c = a ++ b Prelude> c [1,2,3,4,5,6] |
Ruby’s concat |
Js’s concat$ node tests/concat.js
var ar1 = [81, 4, 71, 21];
var ar2 = [5, 37, 99];
var ar3 = ar1.concat(ar2);
{
for (let _a of ar3) {
console.log(_a);
}
}
81 4 71 21 5 37 99 |
Python’s indices
sq = [1, 4, 9, 16, 25]
print('first element:', sq[0])
$ python3 tests/indices.py first element: 1 |
Haskell’s indices$ ghci Prelude> [1, 2, 3, 4, 5, 6] !! 2 3 |
Ruby’s indices |
Js’s indices
const pr = console.log ;
var sq = [1, 4, 9, 16, 25];
{
pr('first element:', sq[0]);
}
$ node tests/indices.js first element: 1 |
Python’s hello
>>> print('hello')
hello
|
Haskell’s hello$ cat hello.hs main = print "Hello" $ ghc hello.hs $ ./hello "Hello" |
Ruby’s hello$ ruby puts "hello" ^D hello ^D is there for Ctrl+D, (end-of-file) |
Js’s hello
$ cat hello.js
console.log("hello");
$ node hello.js hello |
Python’s function>>> def f1(a, b): ... return (a+b) ... >>> f1(2, 4) 6 |
Haskell’s function-- returns a + b add a b = a + b anonym function mapM_ (\d -> putStrLn $ show d) [1,2,3] 1 2 3 |
Ruby’s function
$ cat f1.rb
def f2(a, b)
c = a + b
puts "res: #{c}"
end
# call:
f2(3, 6)
$ ruby f1.rb res: 9 |
Js’s function
// returns a + b
function add(a, b) {
return (a + b);
}
|
Python’s string concat>>> s = "hh" + "kkk" >>> s "hhkkk" |
Haskell’s string concata = "s1 -" b = "- s2" c = a ++ b |
Ruby’s string concat
$ ruby
vs = "hh" + "kkk"
puts "#{vs}"
hhkkk
|
Js’s string concat$ cat n.js var s1 = "abc"; var s2 = "def"; var s3 = s1 + s2; console.log(s3); $ node n.js abcdef |
Python’s if
>>> if 3 > 2:
... print('elo')
...
Ctrl+D elo |
Haskell’s if |
Ruby’s if$ ruby if 2 < 3 puts "h" end ctrl+d h |
Js’s if
if () {
}
|
Python’s else |
Haskell’s else |
Ruby’s else |
Js’s else |
Python’s else-if |
Haskell’s else-if |
Ruby’s else-if |
Js’s else-if |
Python’s |
Haskell’s |
Ruby’s raiseraise an exception if 2 > 3 puts "gt-error" else raise StandardError, "gt-error" end greater-than-error-w/-an-exception |
Js’s |