Introduction to Computer Science I Topic 1: Basic Elements of Programming

download Introduction to Computer Science I Topic 1: Basic Elements  of Programming

of 61

  • date post

    22-Feb-2016
  • Category

    Documents

  • view

    34
  • download

    0

Embed Size (px)

description

Introduction to Computer Science I Topic 1: Basic Elements of Programming. Prof. Dr. Max Mühlhäuser Dr. Guido Rößling. What is Programming?. Let us take a look at what some godfathers of programming have to say:. „To program is to understand“ Kristen Nygaard. - PowerPoint PPT Presentation

Transcript of Introduction to Computer Science I Topic 1: Basic Elements of Programming

Introduction to Computer Science 1

Introduction to Computer Science ITopic 1: Basic Elements of ProgrammingProf. Dr. Max MhlhuserDr. Guido Rling

Telecooperation/RBGTechnische Universitt DarmstadtCopyrighted material; for TUD student use onlyDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T11What is Programming?Let us take a look at what some godfathers of programming have to say:

2To program is to understandKristen NygaardProgramming is a Good Medium for Expressing Poorly Understood and Sloppily Formulated IdeasMarvin Minsky, Gerald J. SussmanDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T12The Elements of ProgrammingA powerful programming language (PL) is more than just a means for instructing a computer to perform tasks

It also serves as a framework within which we organize our ideas about a problem domain

3When we describe a language, we should pay attention to the means that it provides for combining simple ideas to more complex ones.Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T13The Elements of ProgrammingEvery powerful language has three mechanisms to structure ideas about processes:

Primitive expressionsRepresent the simplest entities of the language

Means of combinationCompound elements are built from simpler ones

Means of abstractionCompound elements can be named and manipulated as units4Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T14Keys to Engineering DesignPrimitivesResistors, capacitors, inductors, voltage sources,

Means of combinationRules for how to wire together in a circuitStandard interfaces (e.g. voltages, currents) between elements

Means of abstractionBlack box abstraction think about sub-circuit as a unit: e.g. amplifier, modulator, receiver, transmitter,

5Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T15Language Elements - PrimitivesNumbersExamples: 23, -36Numbers are self-evaluating: the values of the digits are the numbers they denote 23 23-36 -36Boolean valuestrue and false Also self evaluatingNames for built-in proceduresExamples: +, *, /, -, =, What is the value of such an expression?The value of + is a procedureWe will later refer to these kinds of values as first-class proceduresEvaluating by looking up the value associated with the name6Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T16Language Elements - Compound Elements7prefix notation(+ 2 3)left parenthesisOperatorOperandsrightparenthesisThe value of a compound element is determined by executing the procedure (denoted by the operator) with the values of the operands. Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T17Vorteile der Prfix Notation represents the application of the procedure to the numbersLanguage Elements - Compound Elements Compound Elements: A sequence of expressions enclosed in parenthesesthe expressions are primitives or compounds themselvesExample:Expressions representing numbers may be combined with expressions representing a primitive procedure (+ or *) to form a compound expression8(+ 2 3)left parenthesisOperatorOperandsrightparenthesisDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T18Vorteile der Prfix Notation Language Elements - Compound ElementsCan use nested combinationsjust apply rules recursively9A combination always denotes a procedure applicationparentheses cannot be inserted or omitted without changing the meaning of the expression(+ 4 (* 2 3)) = (4 + (2 * 3)) = 10

(* (+ 3 4) (- 8 2)) = ((3 + 4) * (8 - 2)) = 42Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T19Language Elements - Abstractions10Create a complex thing by combining more primitive things, name it, treat it like a primitive.(define score (+ 23 7))(define PI 3.14)Simplest means of abstraction: defineDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T110Language Elements - AbstractionsSpecial form: A bracketed expression, starting with one of the few keywords of scheme

Example: define using define we can pair a name with a valueexample: (define score (+ 23 7))The define special form does not evaluate the second expression (in the example: score)Rather, it pairs that name with the value of the third expression in an environment

The return value of a special form is unspecified11Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T111Naming and the EnvironmentAn important aspect of a programming language is the means it provides to refer to computational objects using names. A name identifies a variable whose value is an object

Environment: the interpreter maintains some sort of memory to keep track of the name-object pairs.Associating values with symbolsRetrieve them later12Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T112Naming and the EnvironmentTo get the value of a name, just look it up in environmentExample: the evaluation of score is 30

(define score (+ 27 3))(define total (+ 30 15))(* 100 (/ score total))

Note: we already did this implictly (looking up a name in an environment) for +, *,

13+*/scoretotal3045Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T113Evaluation RulesSelf evaluating (self-rule) return the valueThe values of digits are the numbers that they nameBuilt-in operator return the machine instruction sequence that carry out the corresponding operations.Name (name-rule) return the value that is associated with that name in the environment.Special form do something special.Combination Evaluate the sub expressions (arbitrary order)Apply the procedure that is the value of the leftmost sub expression (the operator) to the arguments that are the values of the other sub expressions (the operands).

Example of a combination: (+ 4 (* 2 3))14Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T114Selbst komplexe Ausdrucke bearbeitet der Interpreter nach Grundzyklus

Interpreter muss nicht explizit angewiesen werden, den Wert des Ausdrucks auszugeben

Die Regel 2 kann als spezial fall von 3 betrachtet werden

Betonne die Schlsselrole der Umgebung fr die Auswertung

Hebe die Sonderformen hervorEvaluation RulesThe evaluation rule is recursive as one of its steps, the rule needs to invoke itselfEvery element has to be evaluated before the whole evaluation can be done

Evaluating the following combination requires that the evaluation rule be applied to 4 different combinations.15( * (+ 2 (* 4 6) ) (+ 3 5 7) )Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T115Read-Eval-Print-Loopdefine-rule:Only evaluate the second operandThe name of the first operand is bound to the calculated valueThe overall value of the expression is undefined16evaldefine-ruleundefinedprintDifferences betweenScheme versionsName Value

PI3.14PI --> 3.14"Visible worldExecution world(define PI 3.14)Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T116Read-Eval-Print-Loop17232323calculateself-ruleprintExpressionValueprinted representation of the valuePI3.143.14evalname-ruleprintValueNaming-rule: Look-up the value in the currentenvironment using the nameVisible WorldExecution WorldExpressionDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T117Capturing common patternsHere are some common patterns (* 3.14 (* 5 5))(* 3.14 (* 23.2 23.2))(* 3.14 (* x x))

How do we generalize i.e. how to express the idea of circle area computation?18They are instances of a circle area computationDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T118Kurze Wiederholung der bis hier Besprochenes

Prozedurdefinition als eine weitaus leistungsfhigere Abstraktionsmittel: Eine zusammengesetzte Operation einen Namen geben mit dem man auf sie sich beziehen kann.Creating new proceduresWe use procedures to capture ways of doing thingssometimes we also use the name functionsimilar to a function in mathematics

The define special form is used to create new procedures19NameParameter(define (area-of-disk r) (* 3.14 (* r r)))Body of procedureDr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T119Creating new ProceduresAs soon as a procedure has been defined, we can use it as if it were a primitive procedure (such as +, * etc.) Example - area of a circle:(area-of-disk 5) = 78.5

Example - area of a ring:(- (area-of-disk 5) (area-of-disk 3)) = (78.5 - 28.26) = 50.2420=-Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T120 Creating new proceduresExisting procedures can be combined to new, more powerful procedures Example: calculate area of a ring

Example: Using the new procedure

(area-of-ring 5 3) = (- (area-of-disk 5) (area-of-disk 3))= (- (* 3.14 (* 5 5)) (* 3.14 (* 3 3)))= = 50.24 21(define (area-of-ring outer inner) (- (area-of-disk outer) (area-of-disk inner)))Dr. G. RlingProf. Dr. M. MhlhuserRBG / TelekooperationIntroduction to Computer Science I: T121Informal specificationsTypical program specificationUsually not in mathematical terms that can be directly transformed into programs Often rather informal problem specificationsMay contain irrelevant or ambiguous information Example:22Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hour