CS 152 / SE 152 Programming Language Paradigms

Similar documents
CS 152 / SE 152 Programming Language Paradigms

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

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

Programming Languages. Function-Closure Idioms. Adapted from Dan Grossman's PL class, U. of Washington

User-defined Functions. Conditional Expressions in Scheme

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

Sample Final Exam Questions

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

CS61A Midterm 2 Review (v1.1)

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

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

Functional Programming - 2. Higher Order Functions

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

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Fall 2017 December 4, Scheme. Instructions

Functional Programming. Pure Functional Programming

Lisp Basic Example Test Questions

by the evening of Tuesday, Feb 6

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

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

YOUR NAME PLEASE: *** SOLUTIONS ***

Scheme: Strings Scheme: I/O

Fall 2017 December 4, 2017

CSE413 Midterm. Question Max Points Total 100

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

Vectors in Scheme. Data Structures in Scheme

A Brief Introduction to Scheme (II)

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

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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


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

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

Scheme Quick Reference

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

Defining Recursive Procedures. Lecture 8: Recursing Lists. list? Tracing list? sumlist. Defining Recursive Procedures on Lists

CMPE Data Structures and Algorithms in C++ Spring 2018

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 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?

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

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

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

April 2 to April 4, 2018

CS 61A Discussion 8: Scheme. March 23, 2017

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

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

CSE 341 Section Handout #6 Cheat Sheet

Scheme: Expressions & Procedures

Recursion & Iteration

Functional programming with Common Lisp

CS 314 Principles of Programming Languages

Homework 5 ML module language assigned Thursday 20 March, due Friday 4 April

Turtles All The Way Down

CS450 - Structure of Higher Level Languages

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

Wellesley College CS251 Programming Languages Spring, 2000 FINAL EXAM REVIEW PROBLEM SOLUTIONS

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

Scheme Quick Reference

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

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

2.2 Hierarchical Data and the Closure Property

Building a system for symbolic differentiation

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

Laboratory: Recursion Basics

CS3: Introduction to Symbolic Programming. Lecture 14: Lists.

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

Introduction to Scheme

CSE 341 Lecture 16. More Scheme: lists; helpers; let/let*; higher-order functions; lambdas

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CS 314 Principles of Programming Languages. Lecture 16

Data Structures CS 315 Spring 2017

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

CMPE/SE 135 Object-Oriented Analysis and Design

An introduction to Scheme

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

University of Massachusetts Lowell

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Delayed Expressions Fall 2017 Discussion 9: November 8, Iterables and Iterators. For Loops. Other Iterable Uses

Solving Your Problem by Generalization

Richard Feynman, Lectures on Computation

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

cs1120: Exam 1 Due: Wednesday, 06 October at 3:30pm (in class)

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

Programming Systems in Artificial Intelligence Functional Programming

CS 206 Introduction to Computer Science II

Chapter 15. Functional Programming Languages

San Jose State University College of Science Department of Computer Science CS151, Object-Oriented Design, Sections 1, 2, and 3, Spring 2018

Building a system for symbolic differentiation

CMPE 180A Data Structures and Algorithms in C++ Spring 2018

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

Pattern Matching WIlensky Chapter 21

15-110: Principles of Computing, Spring 2018

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

UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed

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

Lambda Calculus see notes on Lambda Calculus

Transcription:

CS 152 / SE 152 Programming Language Paradigms Spring Semester 2014 Department of Computer Science San Jose State University Prof. Ron Mak Assignment #3 Assigned: Monday, February 24 Due: Friday, March 7 at 11:59 pm 100 points, team assignment Symbolic differentiation using infix notation You are provided a Scheme procedure and its helper procedures that perform symbolic differentiation of polynomial expressions using infix notation: d dx ( ax 5 4 3 2 4 3 2 + bx + 2x + 6x + 3x + 7) = 5ax + 4bx + 6x + 12x + 3 (deriv '(a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7) 'x) (5 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3) Write additional procedures evaluate and evaluate-deriv to evaluate f (x) and f '(x), respectively, for any value x, where f is a polynomial function in x and f ' is its derivative with respect to x. Assume a set of values for the coefficients a, b, c, etc. You can add more helper procedures as needed. Procedures evaluate and evaluate-deriv each must take two parameters, f and x, where f is a list that represents the function s polynomial expression in infix notation, an example of which is shown above. x is the value of x to evaluate f or f, respectively. The coefficient values can be defined beforehand, such as with (define a 1) 1

Your procedures evaluate and evaluate-deriv must work at least for the example values below for f, a, and b, evaluated at x bound to 0 and then to 1: (define f '(a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7)) (deriv f 'x) (5 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3) (define a 1) (define b 1) (evaluate f 0) 7 (evaluate f 1) 20 (evaluate-deriv f 0) 3 (evaluate-deriv f 1) 30 You should, of course, test your procedures with other values. Tip Write helper procedures to convert the polynomial expression from infix to prefix notation. What to turn in Create a zip file containing: Text files containing your new Scheme procedures. Text files containing output from the above example values for f, a, b, and x, and other test values. A short report (a few paragraphs) that briefly explains your code design. Email the zip file to ron.mak@sjsu.edu. Some mailers may not allow you to mail zip files, so you may have to rename the file to have the suffix other than.zip, such as.zzz. Do not include executable files. Important: Name your zip file after your team name, such as SuperCoders.zip. Your email subject line should be: CS 152 Assignment #3, team name CC all your team members so I can reply all with your score. 2

; Differentiate an infix polynomial with terms of the form +cax^n ; where c is an optional integer constant and a is an optional ; variable other than x, and optional exponent n is an integer > 0. (3 a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7) ; Differentiate with respect to var, for example x. ; The derivative will be in the same infix form. ; ; Assumptions: ; The polynomial is in the "canonical" form as described above. ; There is no error checking of the form. In particular, only ; the variable we're differentiating with respect to (such as x) ; can have an exponent. Any deviation from canonical form may ; cause a runtime exception. ; ; CS 152 Programming Language Paradigms ; Department of Computer Science ; San Jose State University ; Spring 2014 ; Instructor: Ron Mak ; Find the derivative of polynomial poly with respect to variable var. ; The polynomial must be in canonical infix form. ; First "terminize" the polynomial. ; (terminize '(3 a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7)) ; ==> ((3 a x ^ 5) (b x ^ 4) (2 x ^ 3) (6 x ^ 2) (3 x) (7 ; Note that var is a free variable in local procedure deriv-term. ; At run time, deriv-term obtains the value of var from its closure, ; and it is mapped over each sublist in the terminized polynomial. ; The result is a list of sublists, each sublist representing the derivative ; of the corresponding term. ; (map deriv-term '((3 a x ^ 5) (b x ^ 4) (2 x ^ 3) (6 x ^ 2) (3 x) (7) ; ==> ((15 a x ^ 4) (4 b x ^ 3) (6 x ^ 2) (12 x) (3) (0)) ; (deriv '(3 a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7) 'x) ; ==> (15 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3) (define deriv (lambda (poly var) (let* ((terms (terminize poly)) ; "terminize" the polynomial (deriv-term ; local procedure deriv-term (lambda (term) ((null? term) '()) ((not (member? var term)) '(0)) ; deriv = 0 ((not (member? '^ term)) (upto var term)) ; deriv = coeff (else (deriv-term-expo term var)) ; handle exponent (diff (map deriv-term terms ; map deriv-term over the terms (remove-trailing-plus (polyize diff)) ; finalize the answer 3

; Differentiate a single term that contains the var ; raised to a power (i.e., there's an exponent). ; If there are two adjacent integer constants in the new ; coefficient, replace them by their product. (deriv-term-expo '(3 a x ^ 5) 'x) ==> (15 a x ^ 4) (define deriv-term-expo (lambda (term var) (let* ((coeff (upto var term)) ; get the coefficient (rev-term (reverse term)) ; reverse so that we can (expo (car rev-term)) ; get the exponent at the end (new-expo (sub1 expo)) ; new exponent (new-coeff (if (number? (car coeff)) (cons (* (car coeff) expo) (cdr coeff)) ; product (cons expo coeff) ; The derivative: (if (= new-expo 1) (append new-coeff (list var)) (append new-coeff (list var '^ new-expo ; Convert an infix polynomial into a list of sublists, ; where each sublist is a term. (terminize '(3 a x ^ 5 + b x ^ 4 + 2 x ^ 3 + 6 x ^ 2 + 3 x + 7)) ; ==> ((3 a x ^ 5) (b x ^ 4) (2 x ^ 3) (6 x ^ 2) (3 x) (7 (define terminize (lambda (poly) ((null? poly) '()) (else (cons (upto '+ poly) (terminize (after '+ poly) ; Convert a list of term sublists to an infix polynomial. ; Do not include any + 0 term. ; There may be an extra + at the end. (polyize '((15 a x ^ 4) (4 b x ^ 3) (6 x ^ 2) (12 x) (3) (0 ; ==> (15 a x ^ 4 + 4 b x ^ 3 + 6 x ^ 2 + 12 x + 3 +) (define polyize (lambda (terms) ((null? terms) '()) ((equal? '(0) (car terms)) '()) ((null? (cdr terms)) (car terms)) (else (append (car terms) '(+) (polyize (cdr terms) 4

; Return the polynomial without any extra + at the end. ; If the polynomial is empty, return (0). (define remove-trailing-plus (lambda (poly) (if (null? poly) '(0) (let ((rev-poly (reverse poly (if (equal? '+ (car rev-poly)) (reverse (cdr rev-poly)) (reverse rev-poly)) ) ; Return #t if the given item is a top-level item of the given list. ; Else return #f. (define member? ((null? lst) #f) ((equal? item (car lst)) #t) (else (member? item (cdr lst ; Return a list consisting of the items of the given list ; up to the first occurrence of the given item. ; Used to extract the coefficient of a term. ; Return the original list if the given item isn't in the list. (upto 'x '(3 a x ^ 5)) ==> (3 a) (define upto ((null? lst) '()) ((equal? item (car lst)) '()) (else (cons (car lst) (upto item (cdr lst) ; Return a list consisting of the items of the given list ; after the first occurrence of the given item. ; Used to extract the exponent of a term. ; Return the empty list if the given item isn't in the list. (after 'x '(3 a x ^ 5)) ==> (^ 5) (define after ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) (else (after item (cdr lst 5