Multilingual Programming

Python, Perl, Haskell, Js

Python

Perl

Haskell

Js

Python’s printf

$ cat hello.py
print("Hello")

$ python3 hello.py
Hello

Perl’s printf

$ cat hello.pl
print "Hello\n";

$ perl hello.pl
Hello

Haskell’s printf

$ cat hello.hs
main = putStrLn "Hello!"

$ ghc hello.hs

$ ./hello
Hello!

Js’s printf

$ cat hello.js
console.log("Hello");

$ node hello.js
Hello

Python’s math

Simple 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

Perl’s math

$ perl tests/plus.pl
$a = 2;
$b = 3;
$r = $a + $b;
print $r, "\n";
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

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

Perl’s vars

Scalars:

$ perl
$a = 24;
print $a;
print "\n";
24

Haskell’s vars

v = 3
v :: Int
v = 3
let v = 3

Js’s vars

var 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

Perl’s Arrays

@animals = ("cat", "bird", "mouse");

foreach (@animals) {
  print "This animal is a $_\n";
}
$ perl foreach.pl
This animal is a cat
This animal is a bird
This animal is a mouse

Haskell’s foreach

$ ghci
Prelude> ds = map show [1, 2, 3, 4]
Prelude> mapM_ putStrLn ds
1
2
3
4

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

Perl’s Arrays of arrays

@a = ( [1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]
     );

print $a[1][2], "\n";
$ perl aa.pl
6

Haskell’s Arrays of arrays


Js’s Arrays of arrays

let 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 switch


no switch, use elif

Perl’s switch


no switch, use elsif

Haskell’s switch

$ ghci
Prelude> v = 3
Prelude> :{
Prelude| b = case v of
Prelude|   3 -> "Three"
Prelude|   _ -> "Not three"
Prelude| :}
Prelude> b
"Three"

Js’s switch

switch (ed) { //
  case 37: return 'left' ;
  case 38: return 'up'   ;
  case 39: return 'right';
  case 40: return 'down' ;
}

Python’s concat

sq1 = [1, 4, 9, 16, 25]
sq2 = [36, 49, 64, 81, 100]

sq3 = sq1 + sq2

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Perl’s concat

my @a = (1, 2, 3, 4);
my @b = (5, 6, 7, 8);
my @c = (@a, @b);

foreach my $n (@c) {
  print "$n\n";
}
my @a = (1, 2, 3);
my @b = (4, 5, 6);

push @a, @b;
# @a modified, now is:
# (1, 2, 3, 4, 5, 6)

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]

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

Perl’s indices

my @lst = ("alpha", "beta", "gamma");
$lst[0]

Haskell’s indices

$ ghci
Prelude> [1, 2, 3, 4, 5, 6] !! 2
3

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

Perl’s hello

print "Hello!\n";

Haskell’s hello

$ cat hello.hs
main = print "Hello"

$ ghc hello.hs

$ ./hello
"Hello"

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

Perl’s function

sub f {
  $first = shift;
  $second = shift;
  print $first, "\n";
  print $second, "\n";
}

f("Hello", "Good Bye");
$ perl tests/sub.pl
Hello
Good Bye

Haskell’s function

-- returns a + b
add a b = a + b

anonym function

mapM_ (\d -> putStrLn $ show d) [1,2,3]
1
2
3

Js’s function

// returns a + b
function add(a, b) {
    return (a + b);
}

Python’s string concat

>>> s = "hh" + "kkk"
>>> s
"hhkkk"

Perl’s string concat

$ cat p.pl
my $s = "a_" . "_M";
print $s;

$ perl p.pl
a__M

Haskell’s string concat

a = "s1 -"
b = "- s2"
c = a ++ b

Js’s string concat

$ cat n.js
var s1 = "abc";
var s2 = "def";
var s3 = s1 + s2;
console.log(s3);

$ node n.js
abcdef