Comparisment

   - compare programming languages by example

Translations

This page shows side by side 'translations' of code written in two given programming languages to solve the same problem.
This could be useful for learning a new programming language by comparison to a familiar one.

This page is currently a simple view into the data created on the primary comparisons page and is even more beta.

Translate .

Single line comment

The syntax for a single human readable line of text written inline with source code.

Haskell
Python
-- A single line comment

Two dashes start a line comment.

By: sam


# A single line comment

A hash/ number sign/ pound sign starts a line comment.

By: sam


Block comment

The syntax for a human readable line of text written inline with source code and spanning multiple lines.

Haskell
Python
{- A multiple
   line comment.
-}

Enclose with {- and -} .

By: sam


""" A multiple
    line comment.
"""

Enclose with three double-quote symbols.

By: sam


Integer literals

The syntax for literal integers.

Haskell
Python
1
2 :: Num a => a
3 :: Int

Numeric literals may be used to represent "Int" values. Their type however is more general "Num a => a", meaning the same syntax may be used for any other instance of the Num typeclass such as Float, Double, etc. When ambiguous, a type signature can be given with ":: Int" to specialise the type. Numeric literals have special type defaulting rules meaning that if the type is ambigous then the type is first taken as "Int", and second as "Double". These rules can be overiden by making a "default" declaration.

By: sam


42

By: sam


Float literals

The syntax for literal floating point numbers.

Haskell
Python
1.5

By: sam


1.5

By: sam


Boolean literals

The syntax for literal booleans.

Haskell
Python
t :: Bool
t = True

f :: Bool
f = False

By: sam


t = True

f = False

By: sam


String literals

The syntax for literal strings.

Haskell
Python
greeting :: String
greeting = "Hello World!"

A string is enclosed with double quote marks.

By: sam


greeting :: String
greeting = ['H','e','l','l','o',' ','W','o','r','l','d','!']

A string is a list of characters.

By: sam


greeting = "Hello World!"

A string is enclosed in double quotation marks.

By: sam


greeting = 'Hello World!'

A string may be enclosed in single quotation marks.

By: sam


Character literals

The syntax for literal characters.

Haskell
Python
a :: Char
a = 'a'

A character enclosed in single quotes, NOT double quotes which define a string of characters.

By: sam


a = 'a'
b = "b"

A character is a string of length 1 and is either enclosed by single or double quotes.

By: sam


Addition

Addition of numbers.

Haskell
Python
add :: Int -> Int -> Int
add x y = x + y

'+' is the infix addition operator between two arguments of type "Num a => a" of which "Int" is a defined instance.

By: sam


add :: Num a => a -> a -> a
add x y = x + y

Any instance of the "Num" typeclass can be added.

By: sam


def add(x, y): return x + y

"+" is the infix addition operator.

By: sam


Subtraction

Subtract two numbers

Haskell
Python
subtract :: Int -> Int -> Int
subtract x y = x - y

'-' is the infix subtraction operator between two arguments of type "Num a => a" of which "Int" is a defined instance.

By: sam


subtract :: Num a => a -> a -> a
subtract x y = x - y

Any instance of the "Num" typeclass can be subtracted.

By: sam


def subtract(x, y): return x - y

- is the infix subtraction operator.

By: sam


Multiplication

Multiply two numbers.

Haskell
Python
multiply :: Int -> Int -> Int
multiply x y = x * y

'*' is the infix multiplication operator between two arguments of type "Num a => a" of which "Int" is a defined instance.

By: sam


multiply :: Num a => a -> a -> a
multiply x y = x * y

Any instance of the "Num" typeclass can be multiplied together.

By: sam


def multiply(x, y): return x * y

* is the infix multiplication operator.

By: sam


❮ prev next ❯