Midterm Examination (Sample Solutions), Cmput 325

Similar documents
Lisp Basic Example Test Questions

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Lisp. Versions of LISP

CS61A Midterm 2 Review (v1.1)

Simplifying Logical Formulas

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

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

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

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

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

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

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

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Discussion 4. Data Abstraction and Sequences

Recursion & Iteration

Functional Programming. Pure Functional Languages

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 with Common Lisp

Functional Programming. Pure Functional Languages

CSE413 Midterm. Question Max Points Total 100

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

UMBC CMSC 331 Final Exam

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

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

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Lambda Calculus see notes on Lambda Calculus

MIDTERM EXAMINATION - CS130 - Spring 2005

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

Functional Programming. Pure Functional Programming

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

Documentation for LISP in BASIC

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

Project 2: Scheme Interpreter

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Lecture Notes on Lisp A Brief Introduction

Building a system for symbolic differentiation

Pattern Matching WIlensky Chapter 21

An Explicit-Continuation Metacircular Evaluator

Functional Programming. Contents

Review of Functional Programming

Sample Final Exam Questions

FP Foundations, Scheme

CSc 520 Principles of Programming Languages

19 Machine Learning in Lisp

Functional Programming - 2. Higher Order Functions

User-defined Functions. Conditional Expressions in Scheme

CS 314 Principles of Programming Languages

Imperative, OO and Functional Languages A C program is

Principles of Programming Languages 2017W, Functional Programming

Introduction to Scheme

Common LISP Tutorial 1 (Basic)

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

by the evening of Tuesday, Feb 6

University of Massachusetts Lowell

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

SCHEME AND CALCULATOR 5b

Functions, Conditionals & Predicates

CS 314 Principles of Programming Languages

ALISP interpreter in Awk

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

Imperative languages

Announcements. Today s Menu

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

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

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

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

6.001 recitation 3/21/07

Problem 1: Binary Trees: Flatten

A Quick Introduction to Common Lisp

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

A little bit of Lisp

Structure and Interpretation of Computer Programs

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

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

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

CSCI337 Organisation of Programming Languages LISP

Putting the fun in functional programming

UMBC CMSC 331 Final Exam

Building a system for symbolic differentiation

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

Lecture #24: Programming Languages and Programs

Structure and Interpretation of Computer Programs

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

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

Discussion 12 The MCE (solutions)

Programming Languages

Verifying Centaur s Floating Point Adder

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

A Brief Introduction to Scheme (II)

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?

Department of Computer and information Science Norwegian University of Science and Technology

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

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

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

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming

Streams and Lazy Evaluation in Lisp

Transcription:

Midterm Examination (Sample Solutions, Cmput 325 [15 marks] Consider the Lisp definitions below (defun test (L S (if (null L S (let ((New (add (cdar L S (test (cdr L New (defun add (A S (if (null S (cons (cons A 1 nil (if (eq (caar S A (cons (cons A (+ 1 (cdar S (cdr S (cons (car S (add A (cdr S For each of the following calls, show precisely what will be returned: (add c nil answer: ((c. 1 (add c ((a. 4 (b. 2 (c. 3 answer: ((a. 4 (b. 2 (c. 4 (add d ((a. 4 (b. 2 (c. 3 answer: ((a. 4 (b. 2 (c. 3 (d. 1 (test ((a. b (a. c (b. d nil answer: ((b. 1 (c. 1 (d. 1 (test ((a. b (a. c (b. a (b. b (c. b nil answer: ((b. 3 (c. 1 (a. 1 1

[6 marks] Show the machine level representation for each of the following S-expressions. (a (b c -> -> / V V V a --------- c / --------- V b ((a b. (c d -------------> -> / V V V ------- ------- c d -> / ------ ------- V V a b [4 marks] Show the simplest S-expression that is stored internally by the following structure: --------- -> -> -----------> -> f --------- V V V V a b --------- e -> d --------- V c (a b (c. d e. f 2

[5 marks] The lambda expression for the successor function can be defined as follows: SUC = (λxsz s(xsz Reduce the expression below to its normal form (this exercise intends to show that the successor of three is four. (λxsz s(xsz (λsx s(s(sx (λsz s((λsx s(s(sx sz (λsz s(s(s(sz [6 marks] Recall that boolean constants true and false are defined as: T = (λxy x and F = (λxy y, respectively. Suppose that a boolean operator OP is defined as follows: OP = (λxy (xtf (yft x Draw a truth table for OP. That is, for every possible combination of boolean values X and Y, show the value of (OP X Y. X Y OP X Y ----------------- T T F T F T F T F F F F [2 marks] Suppose a lambda expression has a normal form. Is there any reduction strategy that guarantees the termination? Answer: In this case, normal order reduction guarantees termination. 3

[10 marks] For each lambda expression below, evaluate it by the interpreter based on context and closure, and answer the corresponding questions. The initial context is assumed to be CT0. Clearly indicate your answer. You don t need to show how you get your answer. (1 (lambda (x (g x ((lambda (x (+ x x 3 Show the context when (+ x x is being evaluated. Answer: {x -> 3} U CT0 Show the context when (g x is being evaluated. Answer: {x -> 6} U CT0 (2 ((lambda (f z (f 2 z (lambda (x y (- y x 5 Show the context when (f 2 z is being evaluated. Answer: {f -> [(lambda (x y (- y x, CT0], z -> 5} U CT0 [17 marks] Define the function (defun createpair (L... where L is a list of atoms, and the function replaces each atom a by a pair (a a. E.g., (createpair (a b c = > ((a a (b b (c c. (defun createpair (L (if (null L nil (cons (cons (car L (cons (car L nil (createpair (cdr L Write a single Lisp expression such that, given an L, the expression evaluates to the same result. (mapcar (lambda (x (cons x (cons x nil L Define the function (defun member0 (A L... where A is an atom and L is a (possibly nested list of atoms. The function returns t if A is anywhere in L. E.g. (member0 a (b ((a c d => t Requirement: Your solution must visit evey atom in L at most once. Because of this restriction, you cannot flatten the list first and then apply the member function. 4

(defun member0 (A L (cond ((null L nil ((not (atom L (or (member0 A (car L (member0 A (cdr L (t (eq A L [15 marks] ; Define (insert A L such that A is inserted into L in every possible ; position. The function returns a list of all these extended lists. ; E.g. (insert a (1 2 3 ==> ((a 1 2 3 (1 a 2 3 (1 2 a 3 (1 2 3 a ;---------------------------------------------------------------------- ; Place A into L at the Nth position (defun putat (A L N (if (eq N 1 (cons A L (cons (car L (putat A (cdr L (- N 1 ;calls insert0 with number of positions (defun insert (A L (let ((len (length L (insert0 A L (+ len 1 ; Insert A into L at the Nth position, and recursively at N-1th position ; and so on. (defun insert0 (A L N (if (eq N 0 nil (cons (putat A L N (insert0 A L (- N 1 5