Docsity

Imperative Languages - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

Partial preview of the text

Download Imperative Languages - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Programming Languages

Chapter One Modern Programming Languages, 2nd ed. 1

Outline

 What makes programming languages an

interesting subject?

– The amazing variety

– The odd controversies

– The intriguing evolution

– The connection to programming practice

– The many other connections

Chapter One Modern Programming Languages, 2nd ed. 2

Imperative Languages

 Example: a factorial function in C

 Hallmarks of imperative languages:

– Assignment

– Iteration

– Order of execution is critical

Chapter One Modern Programming Languages, 2nd ed. 4

int fact(int n) {

int sofar = 1;

while (n>0) sofar *= n--;

return sofar;

Functional Languages

 Example: a factorial function in ML

 Hallmarks of functional languages:

– Single-valued variables

– Heavy use of recursion

Chapter One Modern Programming Languages, 2nd ed. 5

fun fact x =

if x <= 0 then 1 else x * fact(x-1);

Logic Languages

 Example: a factorial function in Prolog

 Hallmark of logic languages

– Program expressed as rules in formal logic

Chapter One Modern Programming Languages, 2nd ed. 7

fact(X,1) :-

X =:= 1.

fact(X,Fact) :-

X > 1,

NewX is X - 1,

fact(NewX,NF),

Fact is X * NF.

Object-Oriented Languages

 Example: a Java definition for a kind of

object that can store an integer and compute

its factorial

Chapter One Modern Programming Languages, 2nd ed. 8

Object-Oriented Languages

 Hallmarks of object-oriented languages:

– Usually imperative, plus…

– Constructs to help programmers use

“objects”—little bundles of data that know how

to do things to themselves

Chapter One Modern Programming Languages, 2nd ed. 10

Strengths and Weaknesses

 The different language groups show to

advantage on different kinds of problems

 Decide for yourself at the end of the

semester, after experimenting with them

 For now, one comment: don’t jump to

conclusions based on factorial!

– Functional languages do well on such functions

– Imperative languages, a bit less well

– Logic languages, considerably less well

– Object-oriented languages need larger examples

Chapter One Modern Programming Languages, 2nd ed. 11

Example: Forth Factorial

 A stack-oriented language

 Postscript is similar

 Could be called imperative , but has little in

common with most imperative languages

Chapter One Modern Programming Languages, 2nd ed. 13

: FACTORIAL

1 SWAP BEGIN ?DUP WHILE TUCK * SWAP 1- REPEAT ;

Example: APL Factorial

 An APL expression that computes X’s factorial

 Expands X it into a vector of the integers 1..X,

then multiplies them all together

 (You would not really do it that way in APL, since

there is a predefined factorial operator: !X)

 Could be called functional , but has little in

common with most functional languages

Chapter One Modern Programming Languages, 2nd ed. 14

× / ι X

The Odd Controversies

 Programming languages are the subject of

many heated debates:

– Partisan arguments

– Language standards

– Fundamental definitions

Chapter One Modern Programming Languages, 2nd ed. 16

Language Partisans

 There is a lot of argument about the relative

merits of different languages

 Every language has partisans, who praise it

in extreme terms and defend it against all

detractors

Chapter One Modern Programming Languages, 2nd ed. 17

Basic Definitions

 Some terms refer to fuzzy concepts: all those

language family names, for example

 No problem; just remember they are fuzzy

– Bad: Is X really an object-oriented language?

– Good: What aspects of X support an object-oriented

style of programming?

 Some crisp concepts have conflicting terminology:

one person’s argument is another person’s actual

parameter

Chapter One Modern Programming Languages, 2nd ed. 19

Outline

 What makes programming languages an

interesting subject?

– The amazing variety

– The odd controversies

– The intriguing evolution

– The connection to programming practice

– The many other connections

Chapter One Modern Programming Languages, 2nd ed. 20