Programming Languages

Course Information

Contact Information

Bryan Wilkinson

bwilk1@umbc.edu
bwilk7.github.io
ITE 374 Office Hours: Tuesday 10:00am and Wednesday 2:30pm or by appointment
Website: http://www.csee.umbc.edu/courses/331/fall15/05/

Reasons to Study Programming Languages

  • Increased capacity to express programming concepts
  • Improved background for choosing appropriate languages
  • Enhanced ability to learn new languages
  • Improved understanding of the significance of implementation
  • Increased ability to design new languages
  • Mastering different programming paradigms
import pickle
import numpy

import rpy2.robjects.packages as rpackages
import rpy2.robjects.numpy2ri as np2ri
import rpy2.robjects as ro
np2ri.activate()
psych = rpackages.importr('psych')


rawData = pickle.load(open('mturkResults.pkl'))
data = pandas.DataFrame(rawData)

agreementMatrix = numpy.nan_to_num(numpy.corrcoef(data))
numpy.fill_diagonal(agreementMatrix,1)

#Call R function
factors = psych.fa(agreementMatrix,fm='pa')

Language Evaluation Criteria

  • Readability
  • Writability
  • Reliability
  • Cost

Readability

  • How easy is it to read and understand programs writen in the programming language?
  • Arguably the most important criterion!

Readability Factors

  • Simplicity: too many features is bad
  • Orthogonality: small set of primitive constructs combinable in a small number of ways to build the language’s control and data structures
  • Control statements
  • Data type and structures
  • Syntax considerations

Writeability

  • How easy is it to write programs in the language?
  • Factors effecting writability:
    • Simplicity and orthogonality
    • Support for abstraction
    • Expressivity
    • Fit for the domain and problem

Reliability

  • Type checking
  • Exception handling
  • Aliasing
  • Readability and writability

Cost

  • Programmer training
  • Software creation
  • Compilation
  • Execution
  • Compiler cost
  • Poor reliability
  • Maintenance

Language Design Trade-offs

  • Reliability versus cost of execution
    • Ada, unlike C, checks all array indices to ensure proper range but has very expensive compilation
  • Writability versus readability
    (2 = 0 +.= T o.| T) / T <- iN
    
    • APL one-liner producing prime numbers from 1 to N, obscure to all but the author
  • Flexibility versus safety
    • C, unlike Java, allows one to do arithmetic on pointers

Types of Languages

  • Imperative or procedural
  • Object-oriented
  • Functional
  • Rule based

The following examples are taken from http://rosettacode.org/

Imperative Language: C

#include <stdio.h>
#include <math.h>

int main(int argc, char **argv) {

   float x[4] = {1,2,3,1e11}, y[4];
   int i = 0;
   FILE *filePtr;

   filePtr = fopen("floatArray","w");

   for (i = 0; i < 4; i++) {
      y[i] = sqrt(x[i]);
      fprintf(filePtr, "%.3g\t%.5g\n", x[i], y[i]);
   }

   return 0;
}

Imperative Language: Lua

filename = "file.txt"

x = { 1, 2, 3, 1e11 }
y = { 1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791 };
xprecision = 3;
yprecision = 5;

fstr = "%."..tostring(xprecision).."f ".."%."..tostring(yprecision).."f\n"

fp = io.open( filename, "w+" )

for i = 1, #x do
    fp:write( string.format( fstr, x[i], y[i] ) )
end

io.close( fp )

Object-Oriented Language: Ruby

# prepare test data
x = [1, 2, 3, 1e11]
y = x.collect { |xx| Math.sqrt xx }
xprecision = 3
yprecision = 5

# write the arrays
open('sqrt.dat', 'w') do |f|
  x.zip(y) { |xx, yy| f.printf("%.*g\t%.*g\n", xprecision, xx, yprecision, yy) }
end

# print the result file
open('sqrt.dat', 'r') { |f| puts f.read }

Object-Oriented Language: Java

import java.io.*;

public class FloatArray {
    public static void writeDat(String filename, double[] x, double[] y,
                                int xprecision, int yprecision)
        throws IOException {
        assert x.length == y.length;
        PrintWriter out = new PrintWriter(filename);
        for (int i = 0; i < x.length; i++)
            out.printf("%."+xprecision+"g\t%."+yprecision+"g\n", x[i], y[i]);
        out.close();
    }

    public static void main(String[] args) {
        double[] x = {1, 2, 3, 1e11};
        double[] y = new double[x.length];
        for (int i = 0; i < x.length; i++)
            y[i] = Math.sqrt(x[i]);

        try {
            writeDat("sqrt.dat", x, y, 3, 5);
        } catch (IOException e) {
            System.err.println("writeDat: exception: "+e);
        }

        try {
            BufferedReader br = new BufferedReader(new FileReader("sqrt.dat"));
            String line;
            while ((line = br.readLine()) != null)
                System.out.println(line);
        } catch (IOException e) { }
    }
}

Functional Language: LISP

(with-open-file (stream (make-pathname :name "filename") :direction :output)
    (let* ((x (make-array 4 :initial-contents '(1 2 3 1e11)))
              (y (map 'vector 'sqrt x))
              (xprecision 3)
              (yprecision 5)
              (fmt (format nil "~~,1,~d,,G~~12t~~,~dG~~%" xprecision yprecision)))
        (map nil (lambda (a b)
                     (format stream fmt a b)) x y)))

Functional Language: Haskell

import System.IO
import Text.Printf
import Control.Monad

writeDat filename x y xprec yprec =
  withFile filename WriteMode $ \h ->
     -- Haskell's printf doesn't support a precision given as an argument for some reason, so we insert it into the format manually:
     let writeLine = hPrintf h $ "%." ++ show xprec ++ "g\t%." ++ show yprec ++ "g\n" in
       zipWithM_ writeLine x y

Logical Language: Prolog

palindrome(Word) :- name(Word,List), reverse(List,List).

Implementation methods

  • Direct execution by hardware
    • e.g., native machine language
  • Compilation to another language
    • e.g., C compiled to Intel Pentium 4 native machine language
  • Interpretation: direct execution by software
    • e.g., csh, Lisp, Python, JavaScript
  • Hybrid: compilation then interpretation
    • Compilation to another language (aka bytecode), then interpreted by a ‘virtual machine’, e.g., Java, Perl
  • Just-in-time compilation
    • Dynamically compile some bytecode to native code (e.g., V8 JavaScript engine)

Compliation

Compilation Process

Interpretation

Interpreter Process

Hybrid

Hybrid Process

In [ ]: