Structure of Programming Languages Lecture 5 CSCI 6636 4536 June, 2017 CSCI 6636 4536 Lecture 10... 1/16 June, 2017 1 / 16
Outline 1 Expressions and Evaluation 2 Control Structures Conditionals Repetition Jumps 3 Homework CSCI 6636 4536 Lecture 10... 2/16 June, 2017 2 / 16
Expressions and Evaluation In Praise of Laziness A result from Lambda Calculus: Outside-in evaluation is strictly more powerful than inside-out: if ( x!= 0) answer = z/x; else answer = z; O-I: Evaluate if (x!= 0) first, then choose one clause. I-O: Evaluate z/x first and bomb with a divide-by-zero error. Every programming language evaluates conditionals outside in. Most languages evaluate everything else inside-out. Modern functional languages use outside-in evaluation consistently; it is called lazy evaluation. Nothing is evaluated until and unless the result is needed. CSCI 6636 4536 Lecture 10... 3/16 June, 2017 3 / 16
Expressions and Evaluation The Functional Philosophy A result of using lazy evaluation is that the sequence in which lines of code are written is not the same as the order in which they are evaluated. (A statement sequence is an illusion the results would be the same if the lines were scrambled.) Instead, evaluation order is determined by how function calls are nested in the code, and by whether local variables and parameters are used at all. Functional languages use dynamic binding (no declarations) to attach names to objects, and allow only one binding during the lifetime of the name. Parameter binding is the primary means naming a value. CSCI 6636 4536 Lecture 10... 4/16 June, 2017 4 / 16
Control Structures Expressions vs. Statements Conditionals Loops Jumps CSCI 6636 4536 Lecture 10... 5/16 June, 2017 5 / 16
Expressions vs. Statements All programming languages have control elements that allow testing and repetition. In statement-based languages, these are usually control statements In functional languages, these are usually control expressions or functions Some languages support both but rely more heavily on one or the other. Every aspect of the syntax for control varies widely from language to language. CSCI 6636 4536 Lecture 10... 6/16 June, 2017 6 / 16
Structured vs. Unconstrained The control capabilities built into a language can be structured or unconstrained. A goto statement or expression is unconstrained: It can go to a target anywhere. Function calls are structured control because the define a controlled interface between two parts of a program and control returns to the point at which the call started. An if...then...else statement or a while loop is structured: it has a defined beginning and end, and control flows through it in a prescribed way. Exceptions and break statements are structured because they can only end up at defined spots in the program. The use of unconstrained control capabilities makes a program hard to debug and harder to maintain. CSCI 6636 4536 Lecture 10... 7/16 June, 2017 7 / 16
Minimal necessary control A minimal level of control is required in a computer language language: the programmer needs a way to write conditional code and to repeat blocks of code. if...then...else and recursion form a minimal adequate basis for a language. Functional languages are built on this foundation. if...then...else, the while loop and statement sequences also form an adequate basis for programming. Procedural languages are built on this foundation. However, many languages of both sorts provide a wide assortment of other control structures for convenience and program clarity. Note: A simple if, without the possibility of an else clause is not an adequate basis for defining a language. CSCI 6636 4536 Lecture 10... 8/16 June, 2017 8 / 16
Conditionals Conditional Semantics A conditional statement or expression consists of a test and two clauses. The first clause (the then clause) is used when the test result is true. The second clause (the else clause) is executed when the result is false. A conditional statement must be evaluated outside-in. The condition must be evaluated first. The result of the condition is then tested and used to select either the then clause or the else clause. This control pattern lets us handle special cases and avoid infinite loops or computations that would crash the program. CSCI 6636 4536 Lecture 10... 9/16 June, 2017 9 / 16
Conditionals Variations on the Conditional Keywords vary: if, cond, WHEN, then, elif, elseif, endif, switch, case, EVALUATE The way of delimiting the clauses varies: parentheses, brackets, case labels, keywords (then, elif, elseif, endif) Conditionals can test one condition or a series of conditions. There can be one test per possible action to execute (as in LISP cond) or one test altogether, followed by a sequence of several possible clauses, chosen based on the outcome of the test, which is a small integer. CSCI 6636 4536 Lecture 10... 10/16 June, 2017 10 / 16
Conditionals Conditional Expressions A conditional expression makes a test and returns a value. It can therefore be used in the middle of any expression, including another conditional expression. The conditional expression was supported by Algol-60. C s conditional expression is <test>? <true-action> : <false-action> Scheme and Lisp provide not-quite-identical versions of Haskell and Miranda provide guarded expressions. cond CSCI 6636 4536 Lecture 10... 11/16 June, 2017 11 / 16
Repetition Loops vs. Recursion A programming language needs a way to specify recursion. This could be done using recursion. It could be done using loops. Modern languages usually support both kinds of repetition, in one way or another. Generally, a loop is faster than a recursion to do the same thing. Often, a recursion is more concise than a loop. CSCI 6636 4536 Lecture 10... 12/16 June, 2017 12 / 16
Repetition Loop Statements A loop statement executes a process that changes memory and/or produces a side-effect. A for-each loop applies an action to every element of an array or list. The general loop (FORTH and C) has a body of code with an exit test potentially anywhere within that body. Restricted loops have an exit test at the top or at the bottom: while and do...while Counted loops: the syntax does not determine the semantics. Issues are: Count up or count down The step size can vary. Exit condition can be: >= or > Some languages put restrictions on changing the loop variable within the loop. Trip counts might be predictable... or not. CSCI 6636 4536 Lecture 10... 13/16 June, 2017 13 / 16
Repetition Loop Expressions A loop expression evaluates a function repeatedly and calculates an answer, often in the form of an array or list. In Scheme: map operates on lists and produces a list as its result. (map 1+ (list 1 2 3 4 5)) ;Value 2: (2 3 4 5 6) In APL any function can be applied to an array and produce an array: A 1 2 3 4 5 B 2 3 ρ ι 6 A + 1 B 2 2 3 4 5 6 2 4 6 8 10 12 CSCI 6636 4536 Lecture 10... 14/16 June, 2017 14 / 16
Jumps Jumps and GO TOs goto: No longer good practice because it has bad effects on translation modularity maintainability break and continue: maintain modular structure and eliminate almost all need for goto s. Some people frown on them because they create one-in-two-out modules. Loop labels and loop exits implement multi-level breaks. Exceptions Superman jumps Difficult to implement because the stack must be cleaned up. They eliminate all remaining need for goto. They must be caught and handled to ensure application stability. CSCI 6636 4536 Lecture 10... 15/16 June, 2017 15 / 16
Homework Homework Read Chapters 8 and 10 1 This expression can be evaluated inside-out or outside in: x=sqrt( 3*z - floor(y)) Which function is called first in an inside-out evaluation? Which is called first in an outside-in evaluation? 2 What is the difference between a control statement and a control expression? 3 List the kinds of loops that are supported in Python. 4 List the jump statements (things like break, continue, goto, and exceptions) that are supported in Ruby. Explain how each one works. 5 After reading the relevant section in the text, explain why unrestricted control statements (goto) make it difficult to maintain a large program. CSCI 6636 4536 Lecture 10... 16/16 June, 2017 16 / 16