Lecture #24: Programming Languages and Programs

Similar documents
Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

SCHEME AND CALCULATOR 5b

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Functional Programming. Pure Functional Programming

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Introduction to Scheme

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Organization of Programming Languages CS3200/5200N. Lecture 11

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Discussion 12 The MCE (solutions)

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Administrivia. Simple data types

Documentation for LISP in BASIC

Building a system for symbolic differentiation

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

Functional Programming. Big Picture. Design of Programming Languages

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Scheme Basics > (butfirst '(help!)) ()

CS 314 Principles of Programming Languages

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Introduction to Functional Programming

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Public-Service Announcement

6.001: Structure and Interpretation of Computer Programs

Building a system for symbolic differentiation

Intro. Scheme Basics. scm> 5 5. scm>

Project 2: Scheme Interpreter

Scheme Quick Reference

LECTURE 16. Functional Programming

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

An Explicit-Continuation Metacircular Evaluator

Scheme as implemented by Racket

CS 360 Programming Languages Interpreters

Modern Programming Languages. Lecture LISP Programming Language An Introduction

1.3. Conditional expressions To express case distinctions like

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

6.001 Notes: Section 8.1

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Concepts of programming languages

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

User-defined Functions. Conditional Expressions in Scheme

SOFTWARE ARCHITECTURE 6. LISP

Programming Languages

Chapter 15. Functional Programming Languages

Functional Programming

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

A Brief Introduction to Scheme (II)

FP Foundations, Scheme

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Chapter 1. Fundamentals of Higher Order Programming

Scheme Quick Reference

Functional Languages. Hwansoo Han

Discussion 11. Streams

Principles of Programming Languages 2017W, Functional Programming

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

CSCC24 Functional Programming Scheme Part 2

Scheme: Expressions & Procedures

4/19/2018. Chapter 11 :: Functional Languages

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

A LISP Interpreter in ML

CSc 520 Principles of Programming Languages

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Functional programming in LISP

Lecture Notes on Lisp A Brief Introduction

Programming Systems in Artificial Intelligence Functional Programming

Principles of Programming Languages

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

CSC 533: Programming Languages. Spring 2015

COP4020 Programming Assignment 1 - Spring 2011

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

61A LECTURE 17 ORDERS OF GROWTH, EXCEPTIONS. Steven Tang and Eric Tzeng July 23, 2013

Symbolic Computation and Common Lisp

Chapter 15. Functional Programming Languages

Chapter 11 :: Functional Languages

CS61A Notes Disc 11: Streams Streaming Along

CS61A Midterm 2 Review (v1.1)

Lisp Basic Example Test Questions

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

CS450 - Structure of Higher Level Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

A brief tour of history

Scheme: Strings Scheme: I/O

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Programming Languages

Transcription:

Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware description languages, where the subject matter is typically finite bit sequences and primitive operations on them that correspond directly to machine instructions or gates,......to high-level notations,such aspython, inwhichthesubject matter can be objects and operations of arbitrary complexity. They may be general-purpose, such as Python or Java, or domainspecific, specialized to particular purposes, such as CSS or XAML. Their implementations may stand alone(as for most implementations of Python or C), or be embedded as a component of a larger system. The universe of implementations of these languages is layered: Python can be implemented in C, which in turn can be implemented in assembly language, which in turn is implemented in machine language, which in turn is implemented with gates. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 1

Metalinguistic Abstraction We ve created abstractions of actions functions and of things classes. Metalinguistic abstraction refers to the creation of languages abstracting description. Programming languages are one example. Programming languages are effective: they can be implemented. These implementations interpret utterances in that language, performing the described computation or controlling the described process. The interpreter may be hardware (interpreting machine-language programs) or software (a program called an interpreter), or (increasingly common) both. To be implemented, though, the grammar and meaning of utterances in the programming language must be defined precisely. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 2

Review (from Lecture 1): What s In A Programming Language? Values: the things programs fiddle with; Primitive operations (on values); Combining mechanisms: glue operations together; Predefined names (the library ); Definitional mechanisms: which allow one to introduce symbolic names and (in effect) to extend the library. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 3

Scheme is a dialect of Lisp: The Scheme Language The only programming language that is beautiful. Neal Stephenson The greatest single programming language ever designed Alan Kay Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 4

Scheme Background Invented in the 1970s by Guy Steele ( The Great Quux ), who has also participated in the development of Emacs, Java, and Common Lisp. Designed to simplify and clean up certain irregularities in Lisp dialects at the time. Used in a fast Lisp compiler (Rabbit). Still maintained by a standards committee(although both Brian Harvey and I agree that recent versions have accumulated an unfortunate layer of cruft). Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 5

Values We divide Scheme data into atoms and pairs. The classical atoms: Numbers: integer, floating-point, complex, rational. Symbols. Booleans: #t, #f. The empty list: (). Procedures (functions). Some newer-fangled, mutable atoms: Vectors: Python lists. Strings. Characters: Like Python 1-element strings. Pairs are two-element tuples, where the elements are (recursively) Scheme values. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 6

Symbols Lisp was originally designed to manipulate symbolic data: e.g., formulae as opposed merely to numbers. Such data is typically recursively defined (e.g., an expression consists of an operator and subexpressions ). The basecases hadtoincludenumbers,butalsovariablesorwords. For this purpose, Lisp introduced the notion of a symbol: Essentially a constant string. Two symbols with the same spelling (string) are always the same object. Confusingly, the reader (the program that reads in Scheme programs and data) converts symbols it reads into lower-case first. The main operation on symbols, therefore, is equality. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 7

Pairs and Lists As we ve seen, one can build practically any data structure out of pairs. The Scheme notation for the pair of values V 1 and V 2 is (V 1. V 2 ) InScheme, themainuseofpairsistobuildlists,definedrecursively like an rlist: The empty list, written (), is a list. The pair consisting of a value V and a list L is a list that starts with V, and whose tail is L. Lists areso prevalentthat thereis a standardabbreviation: You can write (V. ()) as (V), and (... (V. R)) as (...V. R ). By repeated application of these rules, the typical list: (V 1. (V 2. (...(V n. ())...))) becomes just (V 1 V 2... V n ). Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 8

Programs Scheme expressions programs are instances of Lisp data structures ( Scheme is written in Scheme ). At the bottom, numerals, booleans, characters, and strings are expressions that stand for themselves. Most lists stand for function calls: (OP E 1 E n ) asaschemeexpressionmeans evaluateopandthe E 1 (recursively), and then apply the value of OP, which must be a function, to the values of the arguments E i. A few lists, identified by their OP, are special forms, which each have different meanings. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 9

Quotation Since programs are data, we have a problem: suppose you want your program to create a piece of data that happens to look like a program? How do we say, for example, Set the variable x to the threeelement list (+ 1 2) without it meaning Set the variable x to the value 3? The quote special form does this: evaluating (quote E) yields E itself as the value, without treating it like a Scheme expression to be evaluated. >>> (+ 1 2) 3 >>> (quote (+ 1 2)) (+ 1 2) >>> (+ 1 2) ; Shorthand. Converted to (quote (+ 1 2)) (+ 1 2) How about >>> (quote (1 2 (3 4))) ;? Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 10

Quotation Since programs are data, we have a problem: suppose you want your program to create a piece of data that happens to look like a program? How do we say, for example, Set the variable x to the threeelement list (+ 1 2) without it meaning Set the variable x to the value 3? The quote special form does this: evaluating (quote E) yields E itself as the value, without treating it like a Scheme expression to be evaluated. >>> (+ 1 2) 3 >>> (quote (+ 1 2)) (+ 1 2) >>> (+ 1 2) ; Shorthand. Converted to (quote (+ 1 2)) (+ 1 2) How about >>> (quote (1 2 (3 4))) ;? (1 2 (quote (3 4))) Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 10

Symbols When evaluated as a program, a symbol acts like a variable name. Variables are bound in environments, just as in Python, although the syntax differs. To define a new symbol, either use it as a parameter name (later), or use the define special form: (define pi 3.1415926) (define pi**2 (* pi pi)) This (re)defines the symbols in the current environment. The second expression is evaluated first. To assign a new value to an existing binding, use the set! special form: (set! pi 3) Here, pi must be defined, and it is that definition that is changed (not like Python). Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 11

Function Evaluation Function evaluation is just like Python: same environment frames, same rules for what it means to call a user-defined function. To create a new function, we use the lambda special form: >>> ( (lambda (x y) (+ (* x x) (* y y))) 3 4) 25 >>> (define fib (lambda (n) (if (< n 2) n (+ (fib (- n 2) (- n 1)))))) >>> (fib 5) 5 The last is so common, there s an abbreviation: >>> (define (fib n) (if (< n 2) n (+ (fib (- n 2) (- n 1))))) Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 12

Numbers All the usual numeric operations and comparisons: >>> (- (quotient (* (+ 3 7 10) (- 1000 8)) 992) 17) 3 >>> (> 7 2) #t >>> (< 2 4 8) #t >>> (= 3 (+ 1 2) (- 4 1)) #t >>> (integer? 5) #t >>> (integer? a) #f Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 13

Lists and Pairs Pairs (and therefore lists) have a basic constructor and accessors: >>> (cons 1 2) (1. 2) >>> (cons a (cons b ())) (1 2) >>> (define L (a b c)) >>> (car L) a >>> (cdr L) (b c) >>> (cadr L) ; (car (cdr L)) b >>> (cdddr L) ; (cdr (cdr (cdr L))) () And one that is especially for lists: >>> (list (+ 1 2) a 4) (3 a 4) >>> ; Why not just write ((+ 1 2) a 4)? Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 14

Conditionals The basic control structures are the conditionals, which are special forms: >>> (define x 14) >>> (define n 2) >>> (if (not (zero? n)) ; Condition... (quotient x n) ; If condition is not #f... x) ; If condition is #f 7 >>> (and (< 2 3) (> 3 4)) #f >>> (and (< 2 3) ()) () >>> (or (< 2 3) (> 3 4)) #t >>> (or (< 3 2) ()) () Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 15

Traditional Conditionals Traditional Lisp had a more elaborate special form, which Scheme inherited: >>> (define x 5) >>> (cond ((< x 1) small)... ((< x 3) medium)... ((< x 5) large)... (else big)) big Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 16

Binding Constructs: Let Sometimes, you d like to introduce local variables or named constants. The let special form does this: >>> (define x 17) >>> (let ((x 5)... (y (+ x 2)))... (+ x y)) 24 This is a derived form, equivalent to: >>> ((lambda (x y) (+ x y)) 5 (+ x 2)) Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 17

Tail recursion With just thefunctionsand specialforms so far, can writeanything. But there is one problem: how to get an arbitrary iteration that doesn t overflow the execution stack because recursion gets too deep? Scheme requires tail-recursive functions to work like iterations. This means that in this program: (define (fib n) (define (fib1 n1 n2 n) (if (< n 2) n2 (fib1 n2 (+ n1 n2) (- n 1)))) (if (= n 0) 0 (fib1 0 1 n))) Instead of calling fib1 recursively, we replace the call on fib1 with the recursive call. Result: don t need while loops. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 18

Length of a list: Examples (define (length L) (if (null? L) 0 (+ 1 (length (cdr L))))) Tail-recursive length: (define (length L) (define (add-length prev L) (if (null? L) prev (add-length (+ prev 1) (cdr L)))) (add-length 0 L)) Scheme version of getitem : (define (nth k L) (if (= k 0) (car L) (nth (- k 1) (cdr L)))) Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 19

Example: Operating on Scheme Evaluate a Scheme expression containing only numbers and binary +, -, and *: (define (eval E) (if (number? E) E (let ((left (eval (nth 1 E))) (right (eval (nth 2 E))) (op (nth 0 E))) (let ((func (cond ((eq? op +) +) ((eq? op -) -) (#t *)))) (func left right))))) E is an expression, represented as a Scheme value. If it s a number, it evaluates to itself. Otherwise, it must have the form (op left right), where op is one of the symbols +, - or *. We evaluate left and right, find the function that corresponds to op, and apply it. Since this is Scheme we are evaluating (in Scheme), the function associated with the symbol +, e.g., is bound to the symbol +. Last modified: Fri Mar 21 18:56:43 2014 CS61A: Lecture #26 20