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.

Python
Haskell
# A single line comment

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

By: sam


-- A single line comment

Two dashes start 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.

Python
Haskell
""" A multiple
    line comment.
"""

Enclose with three double-quote symbols.

By: sam


{- A multiple
   line comment.
-}

Enclose with {- and -} .

By: sam


Integer literals

The syntax for literal integers.

Python
Haskell
42

By: sam


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


Float literals

The syntax for literal floating point numbers.

Python
Haskell
1.5

By: sam


1.5

By: sam


Boolean literals

The syntax for literal booleans.

Python
Haskell
t = True

f = False

By: sam


t :: Bool
t = True

f :: Bool
f = False

By: sam


String literals

The syntax for literal strings.

Python
Haskell
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


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


Character literals

The syntax for literal characters.

Python
Haskell
a = 'a'
b = "b"

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

By: sam


a :: Char
a = 'a'

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

By: sam


Addition

Addition of numbers.

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

"+" is the infix addition operator.

By: sam


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


Subtraction

Subtract two numbers

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

- is the infix subtraction operator.

By: sam


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


Multiplication

Multiply two numbers.

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

* is the infix multiplication operator.

By: sam


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


❮ prev next ❯