computing unit 1: r basics - wuhornik/comp/comp_unit1.pdf · — every 3gl program must be...

57
Computing Unit 1: R Basics Kurt Hornik September 26, 2018

Upload: others

Post on 22-Mar-2021

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Computing Unit 1:R Basics

Kurt Hornik

September 26, 2018

Page 2: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Overview

� Definitions� Programming Languages� S and R

Slide 2

Page 3: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming

To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax The structure of strings in some language.

Grammar A formal definition of the syntactic structure of a language.Semantics Meaning of a language (relation to the real world).

Slide 3

Page 4: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.

(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax The structure of strings in some language.

Grammar A formal definition of the syntactic structure of a language.Semantics Meaning of a language (relation to the real world).

Slide 3

Page 5: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program

A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax The structure of strings in some language.

Grammar A formal definition of the syntactic structure of a language.Semantics Meaning of a language (relation to the real world).

Slide 3

Page 6: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.

Algorithm A detailed sequence of instructions (actions) used to do aparticular job or solve a given problem.

Programming language An artificial language that is used to generateor to express computer programs.

Language System of symbols used for communication (informationexchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 7: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm

A detailed sequence of instructions (actions) used to do aparticular job or solve a given problem.

Programming language An artificial language that is used to generateor to express computer programs.

Language System of symbols used for communication (informationexchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 8: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.

Programming language An artificial language that is used to generateor to express computer programs.

Language System of symbols used for communication (informationexchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 9: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language

An artificial language that is used to generateor to express computer programs.

Language System of symbols used for communication (informationexchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 10: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.

Language System of symbols used for communication (informationexchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 11: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language

System of symbols used for communication (informationexchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 12: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.

Syntax The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 13: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax

The structure of strings in some language.Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 14: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax The structure of strings in some language.

Grammar A formal definition of the syntactic structure of a language.Semantics Meaning of a language (relation to the real world).

Slide 3

Page 15: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax The structure of strings in some language.

Grammar A formal definition of the syntactic structure of a language.

Semantics Meaning of a language (relation to the real world).

Slide 3

Page 16: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Definitions

Programming To design, write, and test programs.(Computer) Program A computer algorithm.Algorithm A detailed sequence of instructions (actions) used to do a

particular job or solve a given problem.Programming language An artificial language that is used to generate

or to express computer programs.Language System of symbols used for communication (information

exchange). Consists of Syntax and Semantics.Syntax The structure of strings in some language.

Grammar A formal definition of the syntactic structure of a language.Semantics Meaning of a language (relation to the real world).

Slide 3

Page 17: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Programming Languages

Imperative PLs:

� Implicit state: variables� State modification: through assignment (“side effecting”)� Instruction sequencing (begin-end blocks, loops, . . . )

Declarative PLs:

� No implicit state, no assignments� Expression evaluation instead of instruction sequencing� Recursion instead of loops

Slide 4

Page 18: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Programming Languages

Imperative PLs:

� Implicit state: variables� State modification: through assignment (“side effecting”)� Instruction sequencing (begin-end blocks, loops, . . . )

Declarative PLs:

� No implicit state, no assignments� Expression evaluation instead of instruction sequencing� Recursion instead of loops

Slide 4

Page 19: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

First Generation Languages (1GL)

Languages of the first computer systems (1940s).

Raw machine code, i.e. numeric (binary) values interpreted ascommands by the processor.

Example: 00011010 0011 0100 (3 + 4)

Second Generation Languages (2GL)

Aka assembler languages (early 1950s).

Symbolic representation of machine code. Use of macros (placeholderfor a sequence of commands) is common.

Example: ADD 3,4

Slide 5

Page 20: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

First Generation Languages (1GL)

Languages of the first computer systems (1940s).

Raw machine code, i.e. numeric (binary) values interpreted ascommands by the processor.

Example: 00011010 0011 0100 (3 + 4)

Second Generation Languages (2GL)

Aka assembler languages (early 1950s).

Symbolic representation of machine code. Use of macros (placeholderfor a sequence of commands) is common.

Example: ADD 3,4

Slide 5

Page 21: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

First Generation Languages (1GL)

Languages of the first computer systems (1940s).

Raw machine code, i.e. numeric (binary) values interpreted ascommands by the processor.

Example: 00011010 0011 0100 (3 + 4)

Second Generation Languages (2GL)

Aka assembler languages (early 1950s).

Symbolic representation of machine code. Use of macros (placeholderfor a sequence of commands) is common.

Example: ADD 3,4

Slide 5

Page 22: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

First Generation Languages (1GL)

Languages of the first computer systems (1940s).

Raw machine code, i.e. numeric (binary) values interpreted ascommands by the processor.

Example: 00011010 0011 0100 (3 + 4)

Second Generation Languages (2GL)

Aka assembler languages (early 1950s).

Symbolic representation of machine code. Use of macros (placeholderfor a sequence of commands) is common.

Example: ADD 3,4

Slide 5

Page 23: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Third Generation Languages (3GL)

High level languages. Key characteristics:� Easy to understand (compared to assembler)

� System independent (core functionality)� Provide named variables� Provide structure elements (loops, conditions)� Every 3GL program must be translated into machine code prior to

execution, either command by command during execution(interpreter) or as a whole before execution (compiler).

� The 3GL program is called source code, the resulting machine codeobject code.

Slide 6

Page 24: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Third Generation Languages (3GL)

High level languages. Key characteristics:� Easy to understand (compared to assembler)� System independent (core functionality)

� Provide named variables� Provide structure elements (loops, conditions)� Every 3GL program must be translated into machine code prior to

execution, either command by command during execution(interpreter) or as a whole before execution (compiler).

� The 3GL program is called source code, the resulting machine codeobject code.

Slide 6

Page 25: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Third Generation Languages (3GL)

High level languages. Key characteristics:� Easy to understand (compared to assembler)� System independent (core functionality)� Provide named variables

� Provide structure elements (loops, conditions)� Every 3GL program must be translated into machine code prior to

execution, either command by command during execution(interpreter) or as a whole before execution (compiler).

� The 3GL program is called source code, the resulting machine codeobject code.

Slide 6

Page 26: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Third Generation Languages (3GL)

High level languages. Key characteristics:� Easy to understand (compared to assembler)� System independent (core functionality)� Provide named variables� Provide structure elements (loops, conditions)

� Every 3GL program must be translated into machine code prior toexecution, either command by command during execution(interpreter) or as a whole before execution (compiler).

� The 3GL program is called source code, the resulting machine codeobject code.

Slide 6

Page 27: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Third Generation Languages (3GL)

High level languages. Key characteristics:� Easy to understand (compared to assembler)� System independent (core functionality)� Provide named variables� Provide structure elements (loops, conditions)� Every 3GL program must be translated into machine code prior to

execution, either command by command during execution(interpreter) or as a whole before execution (compiler).

� The 3GL program is called source code, the resulting machine codeobject code.

Slide 6

Page 28: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Third Generation Languages (3GL)

High level languages. Key characteristics:� Easy to understand (compared to assembler)� System independent (core functionality)� Provide named variables� Provide structure elements (loops, conditions)� Every 3GL program must be translated into machine code prior to

execution, either command by command during execution(interpreter) or as a whole before execution (compiler).

� The 3GL program is called source code, the resulting machine codeobject code.

Slide 6

Page 29: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

History of 3GLs1950: COBOL (COmmon Business Oriented Language)1955: FORTRAN (FORmula TRANslator)1960: BASIC (Beginners All-purpose Symbolic Instruction Code)1970: PASCAL, MODULA (Niklaus Wirth), C1980: C++, Objective Pascal

Slide 7

Page 30: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Fourth Generation Languages (4GL)

� “Application specific” high-level languages, mostly built arounddatabase systems (late 1970s).

� Powerful set of functions/commands, but slower execution than 3GL.Often vendor-dependent.

� Examples:� Query languages for interactive data retrieval (e.g., SQL)� Report generators� Graphics languages (e.g., PostScript)� Application generators, CASE tools (e.g., Delphi)� Very high-level programming languages (e.g., MATLAB, SAS)

Slide 8

Page 31: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Fourth Generation Languages (4GL)

� “Application specific” high-level languages, mostly built arounddatabase systems (late 1970s).

� Powerful set of functions/commands, but slower execution than 3GL.Often vendor-dependent.

� Examples:� Query languages for interactive data retrieval (e.g., SQL)� Report generators� Graphics languages (e.g., PostScript)� Application generators, CASE tools (e.g., Delphi)� Very high-level programming languages (e.g., MATLAB, SAS)

Slide 8

Page 32: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Fourth Generation Languages (4GL)

� “Application specific” high-level languages, mostly built arounddatabase systems (late 1970s).

� Powerful set of functions/commands, but slower execution than 3GL.Often vendor-dependent.

� Examples:� Query languages for interactive data retrieval (e.g., SQL)� Report generators� Graphics languages (e.g., PostScript)� Application generators, CASE tools (e.g., Delphi)� Very high-level programming languages (e.g., MATLAB, SAS)

Slide 8

Page 33: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.

� Each object is composed of data and code which are “encapsulated”from the other objects.

� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.� Examples: C++, Java

Slide 9

Page 34: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.

� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.� Examples: C++, Java

Slide 9

Page 35: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.� An object is characterized through state and behavior.

� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.� Examples: C++, Java

Slide 9

Page 36: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.

� The state of an object is defined by its attributes (variables).Attributes are accessed through methods.

� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.� Examples: C++, Java

Slide 9

Page 37: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.

� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.� Examples: C++, Java

Slide 9

Page 38: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.� Objects are instances of classes (object templates).

� Classes can be hierarchically organized through inheritance of bothmethods and attributes.

� Examples: C++, Java

Slide 9

Page 39: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.

� Examples: C++, Java

Slide 9

Page 40: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Imperative PLs

Object-Oriented Programming Languages

� Objects model real-world entities.� Each object is composed of data and code which are “encapsulated”

from the other objects.� An object is characterized through state and behavior.� The behavior of an object is defined by its repertoire of methods.� The state of an object is defined by its attributes (variables).

Attributes are accessed through methods.� Objects are instances of classes (object templates).� Classes can be hierarchically organized through inheritance of both

methods and attributes.� Examples: C++, Java

Slide 9

Page 41: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Functional Programming Languages

� Computation based on function evaluation.

� Ideally, no assignments (“side-effects”).� Referential transparency: meaning of the whole is solely determined

by the meaning of the parts.� Functions are first-class objects (treated like values)� Lazy evaluation: expressions are evaluated only when needed� Examples: LISP, APL, S

Slide 10

Page 42: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Functional Programming Languages

� Computation based on function evaluation.� Ideally, no assignments (“side-effects”).

� Referential transparency: meaning of the whole is solely determinedby the meaning of the parts.

� Functions are first-class objects (treated like values)� Lazy evaluation: expressions are evaluated only when needed� Examples: LISP, APL, S

Slide 10

Page 43: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Functional Programming Languages

� Computation based on function evaluation.� Ideally, no assignments (“side-effects”).� Referential transparency: meaning of the whole is solely determined

by the meaning of the parts.

� Functions are first-class objects (treated like values)� Lazy evaluation: expressions are evaluated only when needed� Examples: LISP, APL, S

Slide 10

Page 44: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Functional Programming Languages

� Computation based on function evaluation.� Ideally, no assignments (“side-effects”).� Referential transparency: meaning of the whole is solely determined

by the meaning of the parts.� Functions are first-class objects (treated like values)

� Lazy evaluation: expressions are evaluated only when needed� Examples: LISP, APL, S

Slide 10

Page 45: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Functional Programming Languages

� Computation based on function evaluation.� Ideally, no assignments (“side-effects”).� Referential transparency: meaning of the whole is solely determined

by the meaning of the parts.� Functions are first-class objects (treated like values)� Lazy evaluation: expressions are evaluated only when needed

� Examples: LISP, APL, S

Slide 10

Page 46: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Functional Programming Languages

� Computation based on function evaluation.� Ideally, no assignments (“side-effects”).� Referential transparency: meaning of the whole is solely determined

by the meaning of the parts.� Functions are first-class objects (treated like values)� Lazy evaluation: expressions are evaluated only when needed� Examples: LISP, APL, S

Slide 10

Page 47: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Logic Programming Languages

� based on rules of formal logic

� results are derived from rules� base concept: unification

Two terms to be unified are compared. Both constants: result is TRUEor FALSE. One constant, one variable: variable is bound to constant.Two expressions: unified recursively.

� Example: PROLOG (PROgramming with LOGic)

Slide 11

Page 48: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Logic Programming Languages

� based on rules of formal logic� results are derived from rules

� base concept: unification

Two terms to be unified are compared. Both constants: result is TRUEor FALSE. One constant, one variable: variable is bound to constant.Two expressions: unified recursively.

� Example: PROLOG (PROgramming with LOGic)

Slide 11

Page 49: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Logic Programming Languages

� based on rules of formal logic� results are derived from rules� base concept: unification

Two terms to be unified are compared. Both constants: result is TRUEor FALSE. One constant, one variable: variable is bound to constant.Two expressions: unified recursively.

� Example: PROLOG (PROgramming with LOGic)

Slide 11

Page 50: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Declarative PLs

Logic Programming Languages

� based on rules of formal logic� results are derived from rules� base concept: unification

Two terms to be unified are compared. Both constants: result is TRUEor FALSE. One constant, one variable: variable is bound to constant.Two expressions: unified recursively.

� Example: PROLOG (PROgramming with LOGic)

Slide 11

Page 51: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

COBOL

IDENTIFICATION DIVISION.PROGRAM-ID. DisplayNumbers.

DATA DIVISION.WORKING-STORAGE SECTION.01 I PIC 99 VALUE 1.

PROCEDURE DIVISION.Begin.PERFORM UNTIL I = 11DISPLAY IADD 1 TO I

END-PERFORMSTOP RUN.

Slide 12

Page 52: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

FORTRAN

PROGRAM DisplayNumbers

INTEGER :: i

DO 99 i = 1, 10PRINT *, i

99 CONTINUE

END PROGRAM

Slide 13

Page 53: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

BASIC

10 FOR i = 1 TO 1020 PRINT i30 NEXT i

Slide 14

Page 54: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

PASCAL

Program DisplayIntegers;Var i : Integer;BeginFor i := 1 to 10 doWriteLn(i);

End.

Slide 15

Page 55: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

C

void main() {for (int i = 1; i < 10; i++)printf("%u\n", i);

}

Slide 16

Page 56: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

LISP

(dotimes (i 10)(print (+ 1 i))

)

Slide 17

Page 57: Computing Unit 1: R Basics - WUhornik/Comp/comp_unit1.pdf · — Every 3GL program must be translated into machine code prior to execution, either command by command during execution

Examples: 1, 2, 3, . . . , 10

S

print(1 : 10)

Slide 18