# Overview. CS301 Session 3. Problem set 1. Thinking recursively

Size: px
Start display at page:

Transcription

1 Overview CS301 Session 3 Review of problem set 1 S-expressions Recursive list processing Applicative programming A look forward: local variables 1 2 Problem set 1 Don't use begin unless you need a side effect: (if (< i (+ (/ n 2) 1)) (begin! (if (= (mod n i) 0)! 0! (primer n (+ i 1)))) 1) is the same as (if (< i (+ (/ n 2) 1))! (if (= (mod n i) 0)! 0! (primer n (+ i 1))) 1) Thinking recursively (define sumprimes (n) (if (= n 0) 0 (+ (nthprime n) (sumprimes (- n 1))))) versus ;n is desired number, m is current number, x is value of current, y is the sum (define sumprimehelper (n m x y) (if (= n m) y (if (= (prime x) 1)! (sumprimehelper n (+ m 1) (+ x 1) (+ y x) )! (sumprimehelper n m (+ x 1) y)))) (define sumprimes (n) (sumprimehelper n 0 2 0)) ;1st prime is 2 3 4

2 Extending Impcore to uscheme A more general kind of value: the S-expression (Simplified) concrete syntax: literal ::= integer #t #f 'S-exp S-exp ::= literal symbol (S-exp1... S-expn) Creators '(), list1, list2,... Producers List processing cons, append, reverse,... Observers car, cdr, null?, caar, cadr, cdar, Linear recursion on lists Accumulating parameters A simple pattern: (define some-function (l...) (if (null? l) (base-case) (... (some-function (cdr l)...))) ) E.g. length, equal?, append, reverse Most of you know this method and applied it in problem set 1 Improving naive reverse: (define revapp (l1 l2) (if (null? l1) l2 (revapp (cdr l1) (cons (car l1) l2)))) (define reverse (l)?) 7 8

3 Using lists - primes (define remove-multiples (n l) (if (null? l) '() (if (divides n (car l)) (remove-multiples n (cdr l)) (cons (car l) (remove-multiples n (cdr l))) ))) Removing non-primes: Primes continued (define sieve (l) (if (null? l) '() (cons (car l) (sieve (remove-multiples (car l) (cdr l)))))) (define primes<= (n) (sieve (seq 2 n))) 9 10 Insertion sort (define insert (x l) (if (null? l) (list1 x) (if (< x (car l)) (cons x l) (cons (car l) (insert x (cdr l)))))) (define sort (l) (if (null? l) '() (insert (car l) (sort (cdr l))))) Invariant: no duplicate elements (define add-element (x s) (if (member? x s) s (cons x s)))...simple definition of size: Lists as sets (define size (s) (length s)) 11 12

4 Sets continued Sets are egalitarian!...complicated definition of union (define union (s1 s2) (if (null? s1) s2 (add-element (car s1) (union (cdr s1) s2)))) Why not use append? Anything can be a member of a "set": numbers, symbols, lists of all kinds E.g. ->(val s (add-element '(a b) (add-element 3 emptyset))) ((a b) 3) Lists as maps: a-lists General form ((key value) (key value)...) Keys and values can be anything Lookup by linear search bind and find create and use maps Here's the definition of bind A mental exercise (define bind (x y alist) (if (null? alist) (list1 (list2 x y)) (if (equal? x (caar alist)) (cons (list2 x y) (cdr alist)) (cons (car alist) (bind x y (cdr alist)))))) What's the result of (bind 'A 'B alist) if alist is ((C D) (B C) (A E))? 15 16

5 Property lists Applicative programming We can bind a key in an a-list to another a-list! This was very exciting in the early days of knowledge representation research. (define property (x p plist) (find p (find x plist)))...works by applying functions, not by mutating state The meaning of a name in a given scope doesn't change over time - no set Therefore we can have confidence in some simple laws If all subexpressions are applicative (car (cons x y)) = x (cdr (cons x y)) = y Some laws Why do we need the caveat? -> (val x 0) 0 -> (cdr (cons (set x (+ x 1)) (set x (+ x 2)))) 3 -> (val x 0) 0 -> (set x (+ x 2)) 2 cons makes pairs! We can cons anything to anything -> (cons 1 2) (1. 2) -> (cons 1 '(2)) (1 2)...so beware! Normally you want the second thing to be a list

6 (pair? (cons x y)) = #t (null? (cons x y)) = #f (pair? '()) = #f (null? '()) = #t (car '()) ==> ERROR (cdr '()) ==> ERROR More laws Laws for Booleans Again, if all subexpressions are applicative (if #t x y) = x (if #f x y) = y (if (not p) x y) = (if p y x) Laws for the novice programmer: (if p #t #f) = p (if p #f #t) = (not p) A look ahead Local variables using let-binding E.g., instead of (begin (set r (random)) (if (< r 20)... (if (< r 40)... ))) introduce a fresh variable: (let ((r (random)) (if (< r 20)... (if (< r 40)... ))) Assignment Read R&K 3.5, 3.6, and 3.7 to the beginning of section on page 75. Problem set 2 is due Friday

7 Overview CS301 Session 4 Local variables Recursive tree processing Introduction to first-class functions Preview 1 2 Local variables New syntax; the bindings look like an a-list Three types Simple let binds simultaneously let* binds sequentially letrec binds recursively Simultaneous binding (define foo (x) (let ((x (car x)) (y (cdr x))) (cons y x))) -> (foo '(a b)) ((b). a) -> (foo '((a b) c)) ((c) a b) 3 4

8 Sequential binding (define foo (x) (let* ((x (car x)) (y (cdr x))) (cons y x))) -> (foo '(a b)) error: cdr applied to non-pair a in (cdr x) -> (foo '((a b) c)) ((b) a b) Recursive binding (define foo (x) (letrec ((x (car x)) (y (cdr x))) (cons y x))) -> (foo '(a b)) error: car applied to non-pair () in (car x) 5 6 Local functions (define naive-fib (n) (begin (set naive-fib-count (+ naive-fib-count 1)) (if (< n 2) n (+ (naive-fib (- n 1)) (naive-fib (- n 2)))))) -> (naive-fib 10) 55 -> naive-fib-count 177 Local functions (define fib (n) ;; bottom-up helper function: return a list of ;; fib_i, fib_i-1 (letrec ((fib* (lambda (i)! (begin (set fib-count (+ fib-count 1)) (if (= 1 i) (list2 1 0)!! (let* ((fibs (fib* (- i 1)))!!! (fib_i (car fibs))!!! (fib_i-1 (cadr fibs)))!! (list2 (+ fib_i fib_i-1) fib_i))))))) (car (fib* n)))) -> (fib 10) 55 -> fib-count

9 Trees and recursion Let's do binary search trees. We need a slightly different rep from the textbook's: (1 () (5 (2 () ()) ())) (define empty? (tree) (null? tree)) (define left (node) (cadr node)) (define right (node) (caddr node)) (define label (node) (if (empty? node) node (car node))) (val empty_tree '()) Searching Searching is easy - follow the data structure (define lookup (v t) (if (empty? t) #f (if (= v (label t))? (if (< v (label t))?? )))) 9 10 Also easy Insertion (define insert (v t) (if (empty? t) (list3 v '() '()) (if (< v (label t))?? ))) Examples -> (val t1 (insert 2 (insert 1 (insert 5 empty_tree)))) (5 (1 () (2 () ())) ()) -> (val t2 (insert 2 (insert 5 (insert 1 empty_tree)))) (1 () (5 (2 () ()) ())) -> (lookup 0 t2) #f -> (lookup 5 t2) #t (return to lambda) 11 12

10 Traversals Preorder, "level-order", and so on are left as exercises for the reader ;->...but I will take questions In uscheme we don't need define lambda -> (val plus (lambda (x y) (+ x y))) plus -> (plus 2 3) 5 -> define is "syntactic sugar"; functions are just values, bound in the value environment like other values The power of lambda At the top level, not interesting Used for local function definition, more interesting We can pass functions as parameters...and - more interesting - return them as results Creating functions Remember the binary search trees We can create functions in our programs, e.g. -> (val look_t2 (lambda (v) (lookup v t2))) look_t2 -> (look_t2 5) #t -> (look_t2 7) #f 15 16

11 Functions on the fly Closures -> (define look_in (t) (lambda (v) (lookup v t))) look_in -> (val look_t2 (look_in t2)) <procedure> -> (look_t2 7) #f -> (look_t2 2) #t We know what v means - a formal parameter that will be bound when the function is applied - but what does t mean in (lambda (v) (lookup v t)))? Values for free variables Mutation and closures Answer: it depends on the environment Evaluating a lambda-expression requires capturing the environment in a closure We don't write closures explicitly; the interpreter pairs the lambda-expression with the current environment: (lambda (v) (lookup v t)),{x ()} I just lied to you......we can't bind variables to values in environments Because of assignment, we bind variables to locations Bindings in a closure don't change, contents of locations can 19 20

12 Example A pair of functions that manipulate a counter (val resettable-counter-from (lambda (n) (list2 (lambda () (set n (+ n 1))) (lambda () (set n 0))))) (define step (c) ((car c))) (define reset (c) ((cadr c))) (val ten (resettable-counter-from 10)) (val twenty (resettable-counter-from 20)) -> (step ten) 11 -> (step twenty) 21 Preview of next week Closures for first-class functions Higher-order programming, e.g. (foldl (lambda (n t) (insert n t)) empty_tree '( )))... instead of (insert 3 (insert 4... (insert 1 '()))))) 21 22

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### ;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Page 1/11 (require (lib "trace")) Allow tracing to be turned on and off (define tracing #f) Define a string to hold the error messages created during syntax checking (define error msg ""); Used for fancy

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

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

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

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

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

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

### CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

### 6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane Boning Further Variations on a Scheme Beyond Scheme

### What is tail position?

What is tail position? Tail position is defined inductively: The body of a function is in tail position When(if e1 e2 e3) is in tail position, so are e2 ande3 When(let (...) e) is in tail position, so

### Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

### An Introduction to Scheme

An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification

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

Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

### Project 2: Scheme Interpreter

Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

### 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

6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

### 1.3. Conditional expressions To express case distinctions like

Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

### CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

COMPUTER SCIENCE 320 CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination FRIDAY, MARCH 3, 2006 Problem 1. [25 pts.] A special form is an expression that is not evaluated according to the

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

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

CS301 Session 9 The uscheme interpreter Agenda Approaches to solving the homework problem 1 2 Abstract syntax: top level datatype toplevel = EXP of exp DEFINE of name * lambda VAL of name * exp USE of

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

Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas

### An Explicit-Continuation Metacircular Evaluator

Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

### Streams. CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004

Streams CS21b: Structure and Interpretation of Computer Programs Spring Term, 2004 We ve already seen how evaluation order can change behavior when we program with state. Now we want to investigate how

### Project 5 - The Meta-Circular Evaluator

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Spring Semester, 2005 Project 5 - The Meta-Circular

### Project 5 - The Meta-Circular Evaluator

MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 2005 Project 5 - The Meta-Circular

### Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

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

CS61A Notes Week 6B: Streams Streaming Along A stream is an element and a promise to evaluate the rest of the stream. You ve already seen multiple examples of this and its syntax in lecture and in the

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

### 6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

6.821 Programming Languages Handout Fall 2002 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science Problem Set 6 Problem 1: cellof Subtyping Do exercise 11.6

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

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

### (scheme-1) has lambda but NOT define

CS61A Lecture 12 2011-0-11 Colleen Lewis (calc) review (scheme-1) has lambda but NOT define Remember calc-apply? STk> (calc-apply '+ '(1 2 )) 6 STk> (calc-apply '* '(2 4 )) 24 STk> (calc-apply '/ '(10

### COMP 105 Homework: Type Systems

Due Tuesday, March 29, at 11:59 PM (updated) The purpose of this assignment is to help you learn about type systems. Setup Make a clone of the book code: git clone linux.cs.tufts.edu:/comp/105/build-prove-compare

### A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

### Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

### Scheme Quick Reference

Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

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

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

### STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream?

STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? A stream is an element and a promise to evaluate the rest of the stream. Streams are a clever idea that allows one to use sequence

### regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

1 File: regsim.scm Register machine simulator from section 5.2 of STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS This file can be loaded into Scheme as a whole. Then you can define and simulate machines

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

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### LECTURE 16. Functional Programming

LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

### CS450 - Structure of Higher Level Languages

Spring 2018 Streams February 24, 2018 Introduction Streams are abstract sequences. They are potentially infinite we will see that their most interesting and powerful uses come in handling infinite sequences.

### TAIL RECURSION, SCOPE, AND PROJECT 4 11

TAIL RECURSION, SCOPE, AND PROJECT 4 11 COMPUTER SCIENCE 61A Noveber 12, 2012 1 Tail Recursion Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how they relate to iteration

### 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?

More Scheme CS 331 Quiz 1. What is (car ((2) 3 4))? (2) 2. What is (cdr ((2) (3) (4)))? ((3)(4)) 3. What is (cons 2 (2 3 4))? (2 2 3 4) 4. What is the length of the list (()()()())? 4 5. Which element

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018 Review: Defining Scheme Functions (define ( lambda (

### Data! Two new kinds of data. The automatically managed cons cell Mother of heap-allocated data structures

Data! Two new kinds of data The automatically managed cons cell Mother of heap-allocated data structures The function closure Not just a datum: a new feature The setting: Scheme c Copyright 2008 Norman

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

### Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

Fundamentals of Computer Science I (CS151.01 2006F) Quicksort Summary: In a recent reading, you explored merge sort, a comparatively efficient algorithm for sorting lists or vectors. In this reading, we

### What is tail position?

What is tail position? Tail position is defined inductively: The body of a function is in tail position When(if e1 e2 e3) is in tail position, so are e2 ande3 When(let (...) e) is in tail position, so

### Principles of Programming Languages

Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

### Fall 2017 December 4, Scheme. Instructions

CS 61A Final Review Fall 2017 December 4, 2017 Instructions Form a small group. Start on the first problem. Check off with a helper or discuss your solution process with another group once everyone understands

### Fall 2017 December 4, 2017

CS 61A Final Review Fall 2017 December 4, 2017 Instructions Form a small group. Start on the first problem. Check off with a helper or discuss your solution process with another group once everyone understands

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

### Functional Programming - 2. Higher Order Functions

Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

### STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

STREAMS 10 COMPUTER SCIENCE 61AS Basics of Streams 1. What is a stream? 2. How does memoization work? 3. Is a cons-stream a special form? Practice with Streams 1. Define a procedure (ones) that, when run

### ALISP interpreter in Awk

ALISP interpreter in Awk Roger Rohrbach 1592 Union St., #94 San Francisco, CA 94123 January 3, 1989 ABSTRACT This note describes a simple interpreter for the LISP programming language, written in awk.

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

### Notes on Higher Order Programming in Scheme. by Alexander Stepanov

by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

### Lisp Basic Example Test Questions

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

### Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Vectors in Scheme. Data Structures in Scheme

Data Structures in Scheme Vectors in Scheme In Scheme, lists and S-expressions are basic. Arrays can be simulated using lists, but access to elements deep in the list can be slow (since a list is a linked

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

.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

### cs61amt2_4 CS 61A Midterm #2 ver March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name

CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

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

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### CS61A Notes Disc 11: Streams Streaming Along

CS61A Notes Disc 11: Streams Streaming Along syntax in lecture and in the book, so I will not dwell on that. Suffice it to say, streams is one of the most mysterious topics in CS61A, trust than whatever

### April 2 to April 4, 2018

MORE SCHEME COMPUTER SCIENCE MENTORS 61A April 2 to April 4, 2018 1 Scheme 1. What will Scheme output? Draw box-and-pointer diagrams to help determine this. (a) (cons (cons 1 nil) (cons 2 (cons (cons 3

### Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Agenda CS301 Session 11 Discussion: midterm exam - take-home or inclass? Interlude: common type constructors Type soundness 1 2 Things we could add to Impcore Common type constructors Array is a type constructor,

### Simplifying Logical Formulas

Simplifying Logical Formulas Assume we have a SCHEME logical expression involving the operators and, or, not, and the truth constants #t and (). Example: (and a (not b) (or b c) (not (and c (not d))) #t

### 6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

### From Syntactic Sugar to the Syntactic Meth Lab:

From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation

### Evaluating Scheme Expressions

Evaluating Scheme Expressions How Scheme evaluates the expressions? (procedure arg 1... arg n ) Find the value of procedure Find the value of arg 1 Find the value of arg n Apply the value of procedure

### 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))

SCHEME 0 COMPUTER SCIENCE 6A July 26, 206 0. Warm Up: Conditional Expressions. What does Scheme print? scm> (if (or #t (/ 0 (/ 0 scm> (if (> 4 3 (+ 2 3 4 (+ 3 4 (* 3 2 scm> ((if (< 4 3 + - 4 00 scm> (if

### CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018

CSSE 304 Assignment #13 (interpreter milestone #1) Updated for Fall, 2018 Deliverables: Your code (submit to PLC server). A13 participation survey (on Moodle, by the day after the A13 due date). This is

### Important Example: Gene Sequence Matching. Corrigiendum. Central Dogma of Modern Biology. Genetics. How Nucleotides code for Amino Acids

Important Example: Gene Sequence Matching Century of Biology Two views of computer science s relationship to biology: Bioinformatics: computational methods to help discover new biology from lots of data

### Essentials of Programming Languages Language

Essentials of Programming Languages Language Version 5.3 August 6, 2012 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly

### University of Massachusetts Lowell

University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

### Essentials of Programming Languages Language

Essentials of Programming Languages Language Version 6.90.0.26 April 20, 2018 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

### CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan 1 Com S 342 so far So far we have studied: Language design goals, Basic functional programming, Flat recursion over lists, Notion of Scheme procedures

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Turtles All The Way Down

Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of

### CS 61A Midterm #2 ver March 2, 1998 Exam version: A. Your name. login: cs61a- Discussion section number. TA's name

CS 61A Midterm #2 ver1.03 -- March 2, 1998 Exam version: A Your name login: cs61a- Discussion section number TA's name Look at the edge of your seat. Write your ROOM, seat row and number. Your row number

### This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions.

CS 61A Final Exam May 16, 2008 Your name login: cs61a This exam is worth 70 points, or about 23% of your total course grade. The exam contains 15 questions. This booklet contains 18 numbered pages including

### Building a system for symbolic differentiation

Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

### Streams and Evalutation Strategies

Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions - message passing ( define ( make-account balance

### CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

CS 61A, Fall, 2002, Midterm #2, L. Rowe 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams. a) d) 3 1 2 3 1 2 e) b) 3 c) 1 2 3 1 2 1 2 For each of the following Scheme

### MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

### 6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

6.034 Artificial Intelligence February 9, 2007 Recitation # 1 1 Lists and Trees (a) You are given two lists: (define x (list 1 2 3)) (define y (list 4 5 6)) What would the following evaluate to: (append

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

CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that

### Documentation for LISP in BASIC

Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

YOUR NAME PLEASE: *** SOLUTIONS *** Computer Science 201b SAMPLE Exam 1 SOLUTIONS February 15, 2015 Closed book and closed notes. No electronic devices. Show ALL work you want graded on the test itself.

### UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm Sample s CSC324H1 Duration: 50 minutes Instructor(s): David Liu. No Aids Allowed Name: Student Number: Please read the following guidelines carefully.

### Assignment 1. Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do

University of Toronto Mississauga CSC 324 - Principles of Programming Languages, Fall 2016 Assignment 1 Due Tuesday October 11 at 11pm. No late assignments will be accepted. What to do The questions below

### INTRODUCTION TO SCHEME

INTRODUCTION TO SCHEME PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/110 SCHEME: A FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be passed as