Types of Programming Languages

Overview

Different languages have different purposes, so it makes sense to talk about different kinds, or types, of languages. Some types are:

These types are not mutually exclusive: Perl is both high-level and scripting; C is considered both high-level and system.

Machine Code

Most computers work by executing stored programs in a fetch-execute cycle. Machine code generally features

Machine code is usually written in hex. Example for IA-32:

8B 44 24 04 A9 01 00 00 00 75 0C 69 C0 03 00 00
00 40 E9 08 00 00 00 C1 E0 02 2D 03 00 00 00 C3

Assembly Language

An assembly language is basically just a simplistic encoding of machine code into something more readable. It does add labeled storage locations and jump targets and subroutine starting addresses, but not much more. For the IA-32:

	global	_f

_f:     mov     eax, [esp+4]
        test    eax, 1
        jnz     odd
        imul    eax, 3
        inc     eax
        jmp     done
odd:    shl     eax, 2
        sub     eax, 3
done:   ret

For the SPARC:

        .global f
f:
        andcc   %o0, 1, %g0
        bne     .L1
        sll     %o0, 2, %g2
        sll     %o0, 1, %g2
        add     %g2, %o0, %g2
        b       .L2
        add     %g2, 1, %o0
.L1:
        add     %g2, -3, %o0
.L2:
        retl
        nop

High-Level Languages

A high-level language gets away from all the constraints of a particular machine. HLLs have features such as:

The previous example looks like this in Fortran:

       INTEGER FUNCTION F(N)
       INTEGER N
       IF (MOD(N, 2) .EQ. 0) THEN
           F = 3 * N + 1
       ELSE
           F = 4 * N - 3
       END IF
       RETURN
       END

and like this in Ada:

function F (N: Integer) return Integer is
begin
    if N mod 2 = 0 then
        return 3 * N + 1;
    else
        return 4 * N - 3;
    end if;
end F;

and like this in C and C++:

int f(const int n) {
    return (n % 2 == 0) ? 3 * n + 1 : 4 * n - 3;
}

and like this in Java and C#:

class ThingThatHoldsTheFunctionUsedInTheExampleOnThisPage {
    public static int f(int n) {
        return (n % 2 == 0) ? 3 * n + 1 : 4 * n - 3;
    }
}

and like this in JavaScript:

function f(n) {
    return (n % 2 == 0) ? 3 * n + 1 : 4 * n - 3;
}

and like this in Smalltalk:

f
  ^self % 2 = 0 ifTrue:[3 * self + 1] ifFalse:[4 * self - 3]

and like this in ML:

fun f n = if n mod 2 = 0 then 3 * n + 1 else 4 * n - 3

and like this in Lisp and Scheme:

(defun f (n)
   (if (= (mod n 2) 0)
       (+ (* 3 n) 1)
       (- (* 4 n) 3)))

and like this in Prolog:

f(N, X) :- 0 is mod(N, 2), X is 3 * N + 1.
f(N, X) :- 1 is mod(N, 2), X is 4 * N - 3.

and like this in Perl:

sub f {
    my $n = shift;
    $n % 2 == 0 ? 3 * $n + 1 : 4 * $n - 3;
}

and like this in Python (2.5 or greater):

def f(n):
    return 3 * n + 1 if n % 2 == 0 else 4 * n - 3
and like this in Ruby:

def f(n)
  n % 2 == 0 ? 3 * n + 1 : 4 * n - 3;
end
Exercise: Which of these languages required that variables or functions be declared with types and which did not?
Exercise: Implement this function in PHP, Objective C, Scala, D, and Mercury.

System Languages

System programming languages differ from application programming languages in that they are more concerned with managing a computer system rather than solving general problems in health care, game playing, or finance. System languages deal with:

Scripting Languages

Scripting languages are used for wiring together systems and applications at a very high level. They are almost always extremely expressive (they do a lot with very little code) and usually dynamic (the compiler does little, the run-time system does almost everything).

Esoteric Languages

An esoteric language is one not intended to be taken seriously. They can be jokes, near-minimalistic, or despotic (purposely obfuscated or non-deterministic).

See Wikipedia's article on esoteric languages.

Exercise: Implement the function above in False, Brainfuck, Befunge, Malbolge, Kipple, and reMorse.