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

Similar documents
Sample midterm 1 #1. Problem 1 (What will Scheme print?).

CS61A Midterm 2 Review (v1.1)


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

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

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

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Functional Programming. Pure Functional Programming

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

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

CSE413 Midterm. Question Max Points Total 100

TAIL RECURSION, SCOPE, AND PROJECT 4 11

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

User-defined Functions. Conditional Expressions in Scheme

Documentation for LISP in BASIC

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

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

Discussion 4. Data Abstraction and Sequences

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

A Brief Introduction to Scheme (II)

Discussion section number. a b c d e f g h i j k l m n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

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

CS 314 Principles of Programming Languages

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

CS 314 Principles of Programming Languages. Lecture 16

Remember, this question was mis-worded: you could also add quoted words and sentences in the blanks below. This allowed for a solution to [4] below.

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

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

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

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages

FP Foundations, Scheme

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS3L Summer 2011 Final Exam, Part 2 You may leave when finished; or, you must stop promptly at 12:20

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

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

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

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

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

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

Lecture #24: Programming Languages and Programs

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

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

CS450 - Structure of Higher Level Languages

6.001 Notes: Section 8.1

mk-convert Contents 1 Converting to minikanren, quasimatically. 08 July 2014

April 2 to April 4, 2018

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

Programming Project #4: A Logo Interpreter Summer 2005

Structure and Interpretation of Computer Programs

Structure and Interpretation of Computer Programs

CSc 520. Principles of Programming Languages 7: Scheme List Processing

Functional Programming - 2. Higher Order Functions

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

University of Massachusetts Lowell

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

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

HIERARCHICAL DATA What is a Tree? How is it different from a Deep List? When would you use one over the other?

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

Scheme: Strings Scheme: I/O

Lisp. Versions of 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

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

The Interpreter + Calling Functions. Scheme was designed by people. The Interpreter + Calling Functions. Clickers. Parentheses Matter We asked scheme

Algorithms. AVL Tree

Introduction to Scheme

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

SCHEME AND CALCULATOR 5b

Introduction to Functional Programming

Warm-up and Memoization

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS3 Midterm 1 Fall 2006

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


Project 2: Scheme Interpreter

Programming Languages

CSE341 Spring 2017, Final Examination June 8, 2017

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?

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

CSC 533: Programming Languages. Spring 2015

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Module 9: Binary trees

Building a system for symbolic differentiation

Functional Programming Languages (FPL)

An Explicit-Continuation Metacircular Evaluator

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

A Small Interpreted Language

2.2 Hierarchical Data and the Closure Property

Warm-up! CS 3 Final Review. Predict the output. Predict the Output. Predict the output. Predict the output. What is your favorite color?

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

CS 314 Principles of Programming Languages

Evaluating Scheme Expressions

Fall 2017 December 4, Scheme. Instructions

CS3 Midterm 2 Summer 2008

Lexical vs. Dynamic Scope

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Structure and Interpretation of Computer Programs

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

Transcription:

Scheme Basics > (butfirst '(help!)) () [The butfirst of a *sentence* containing one word is all but that word, i.e., the empty sentence. (BUTFIRST 'HELP!) without the inner parentheses would be butfirst of a word, and would return ELP!, the most common wrong answer. The second most common wrong answer, although we didn't take off for it, was '() - - that is, the correct answer with a quote in front. If you are reading '() in programs as if it were the name of the empty sentence, then you don't understand quoting.] > ((if 3 * +) 4 5) 20 [Since 3 is true, the value of the inner parenthesized expression is the multiplication function.] > (let ((+ -))(+ 8 2)) 6 [Names of primitives are just ordinary variables that happen to have a predefined value, but that can be overridden by a local binding. Notice that that isn't true about names of special forms; you can't say (LET ((DEFINE +)) ) etc.] > (let ((+ -)(- +))(- 8 2)) 10 [Names of primitives are just ordinary variables that happen to have a predefined value, but that can be overridden by a local binding. Notice that that isn't true about names of special forms; you can't say (LET ((DEFINE +))...) etc. Since all the bindings in a LET are done at once, not in sequence, the local value for the name "- " is the *original* meaning of +, not the local meaning.] > ((lambda (z) (- z z)) (random 10)) 0 [Some people said "0 if applicative order, some random value if normal order." That's true, but the point of the question was to see if you know what Scheme does!]

Applicative vs. Normal Order If an expression produces an error, just say "error": if it returns a procedure, just say "procedure." Given the following definitions: (define (marco x) 'done) (define (polo) (polo)) (a) What will be the result of the expression (marco (polo)) in normal order? done. in applicative order? Error. (b) What will be the result of the expression (marco polo) in normal order? done. in applicative order? done Iterative vs Recursive Is the following procedure iterative or recursive: (define (foo sent) (define (foo-helper sent prev) (cond ((empty? (bf sent)) (* (first sent) prev)) (else (se (* (first sent) prev) (foo-helper (bf sent) (first sent)))))) (foo-helper sent 1)) If recursive, rewrite as iterative. If iterative, rewrite as recursive. This is recursive. The iterative version looks like: (define (foo sent) (define (foo-iter sent prev so-far) (cond ((empty? sent) so-far) (else (foo-iter (bf sent) (first sent) (se so-far (* (first sent) prev))))) (foo-iter sent 1 '()))

Boxes and Pointers Draw the box and pointer diagrams for the following expressions >(list (append (list 1 2) (cons 3 (cons 4 '( )))) (cons 5 6)) -> - ----> / +- -+---+ +- -+---+ 5 - --> 6 / 1 - --> 2 - --> 3 - --> 4 / >(cons (cons 1 '( )) (list 2 (list 3))) +---+---+ -> - -----> 2 - --> / +- -+---+ +---+---+ +- -+---+ 1 / 3 / (define x (list (list 1 2 3) (list 5) (cons 6 7))) (define y (map cdr x)) +---+---+ -> - --> / - --> 7 / +- -+---+ 2 - --> 3 /

Orders of Growth 1. (define (garply n) (if (< n 2) n (garply (garply (- n 1))))) Solution: garply is a procedure that takes linear O(n) time. When garply is called with a positive number as argument, garply will call itself with progressively smaller arguments, and so (garply n) will yield (garply (garply (garply (garply...(garply 1))...)))) in n time. (garply 1) takes constant time to evaluate and returns 1, and so each containing procedure call will receive 1 as argument and return 1. The chain collapses to the value 1, and since there are n procedure calls in the chain, each of which takes constant time to evaluate, garply takes 2n total time, or O(n) time, to evaluate. 2. (Define (garply n) (if (< n 2) n (+ (foo n) (garply (- n 1))))) Assuming foo is defined somewhere, please circle TRUE or FALSE. and in one sentence explain your choice. True or False: We have enough information to determine the order of growth of garply. True or False: NO matter how foo is defined, garply will always have an order of growth greater than or equal to THETA(n) True or False: garply has an order of growth in Theta(n^2) if foo is defined as follows: (define (foo n) (if (< n 100) 121 (+ (* n 100) (foo (- n 1)))) Solutions: False, True, True

Data Abstraction Time for some math! We're going to use Scheme to represent polynomials of arbitrary degree. We are going to represent a polynomial as a list, where the first element in the list is the coefficient of the zeroth order term, the second element in the list is the coefficient of the first order term, etc. For example, the polynomial 7 2x + 3x^3 would be represented as the list (7-2 0 3). Assume that we already have the constructor make- polynomial defined as follows: (define (make- poly ls) ls). This seems a little silly but bear with us for now. Write the following utility procedures: 1. get- n, which takes a polynomial and a number n as argument and returns the coefficient of its nth term (ex: (get- n (make- poly '(7-2 0 3)) 1) returns - 2). 2. degree, which takes a polynomial as argument and returns its degree (ex: (degree (make- poly '(7-2 0 3))) returns 3). (define (get-n poly n) (if (= n 0) (car poly) (get-n (cdr poly) (- n 1)))) (define (degree poly) (- (length poly) 1)) Write evaluate- at, which takes a polynomial and a number x as argument and evaluates the polynomial at x. You can use the procedure exp, which takes a number n and another number m and raises n to the m power (ex: (exp 2 3) returns 8). (define (evaluate-at poly x) (define (evaluate-helper count poly x result) (cond ((> count (degree poly)) result) (else (evaluate-helper (+ count 1) poly x (+ (* (get-n poly count) (exp x count)) result))))) (evaluate-helper 0 poly x 0)) What is the current run time of degree? Suppose we want to make degree run in constant time. How would we change our representation to make this possible (keep in mind make- poly should still take a list as argument)? Would we have to rewrite get- n and degree? Degree currently runs in linear time (since it uses length, which is a linear time operation). One way to change our representation is to instead represent a polynomial is a pair, whose car is its degree and whose cdr is a list of its coefficients. Now computing the degree takes a single call to car, which takes constant time. We would have to rewrite get- n and degree, but the key point is that we would not have to rewrite evaluate- at.

Higher Order Write a procedure called product- list that takes in a list of numbers (a1, a2, a3, ) and returns another list (a1, a1a2, a1a2a3, ). You may use higher- order functions and recursion. (define (product-list ls) (if (null? ls) '() (cons (car ls) (map (lambda (num) (* num (car ls))) (product-list (cdr ls)))))) Deep Member Write the predicate procedure, deep- member?, which takes in a word and a deep- list as arguments and returns #t if the word appears somewhere within the list and #f otherwise. (define (deep-member? wd ls) (cond ((null? ls) #f) ((atom? (car ls)) (or (equal? (car ls) wd)) (deep-member? wd (cdr ls)))) (else (or (deep-member? (car ls)) (deep-member? (cdr ls)))))) Trees A Tree is considered degenerate if every parent node has at most one child. Write the predicate is- degenerate? which takes a Tree as argument and returns true if the Tree is degenerate, and false otherwise. (define (is-degenerate? Tree) (cond ((null? Tree) #t) ((null? (children Tree)) #t) ((> (length (children Tree)) 1) #f) (else (is-degenerate? (car (children Tree))))) Binary Search Trees Consider a procedure named get- rank for binary search trees. Get- rank takes two arguments, a BST, and a number n, and returns the number of elements in the BST that are smaller than n. Assume that we already have a procedure count- nodes written, which takes a BST and returns the number of nodes in the BST. (define (get-rank Bst n) (cond ((null? Bst) 0) ((< n (datum Bst)) (get-rank (left-subtree Bst) n)) (else (+ 1 (get-rank (right-subtree Bst) n) (count-nodes (left-subtree Bst)))) Also count- nodes can be written as: (define (count-nodes Bst) (if (null? Bst) 0 (+ 1 (count-nodes (left-subtree Bst)) (count-nodes (right-subtree Bst)))))