CS403: Programming Languages. Assignment 3. Rought Draft

Similar documents
CS403: Programming Languages. Assignment 2. Version 1c

CS403: Programming Languages. Assignment 1. Version 2a

Streams and Evalutation Strategies

Functional Programming. Pure Functional Programming

An introduction to Scheme

A Brief Introduction to Scheme (II)

User-defined Functions. Conditional Expressions in Scheme

CSE303 Logic Design II Laboratory 01

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

Documentation for LISP in BASIC

Scheme Quick Reference

Digital Logic Design Exercises. Assignment 1

Introduction to Functional Programming

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

Lecture 19: Functions, Types and Data Structures in Haskell

Functional Programming - 2. Higher Order Functions

Project 1: Part 1. Project 1 will be to calculate orthogonal polynomials. It will have several parts. x = b± b 2 4ac., x 2 = b b 2 4ac.

Scheme Quick Reference

COP4020 Programming Assignment 1 - Spring 2011

Module 3: New types of data

Functional Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

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

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

Problem Set 4: Streams and Lazy Evaluation

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

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

4.2 Variations on a Scheme -- Lazy Evaluation

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

Topics. Computer Organization CS Exam 2 Review. Infix Notation. Reverse Polish Notation (RPN)

1.3. Conditional expressions To express case distinctions like

Labview. Masood Ejaz

CSU2510H Exam 1 Practice Problems

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

Outline. half adders adder circuits. the while loop the for loop. Euclid s algorithm approximating π

A Small Interpreted Language

01 Introduction to Digital Logic. ENGR 3410 Computer Architecture Mark L. Chang Fall 2006

Week 7: Assignment Solutions

Project 2: Scheme Interpreter

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

Programming Assignment 2

Scheme: Expressions & Procedures

Combinational Logic II

ECEN 468 Advanced Logic Design

The design recipe. Readings: HtDP, sections 1-5. (ordering of topics is different in lectures, different examples will be used)

Project 1: Part 1. x = b± b 2 4ac., x 2 = b b 2 4ac.

12 Abstract Data Types

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Lisp Basic Example Test Questions

Lecture #21 March 31, 2004 Introduction to Gates and Circuits

Our Strategy for Learning Fortran 90

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

Class Structure. Prerequisites

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

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

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

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. Big Picture. Design of Programming Languages

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Cpt S 122 Data Structures. Introduction to C++ Part II

Spring 2018 Updates. Computing Technology for All. Data Structure Essentials. Digital Design

Parallel logic circuits

01 Introduction to Digital Logic. ENGR 3410 Computer Architecture Mark L. Chang Fall 2008

GC03 Boolean Algebra

Pace University. Fundamental Concepts of CS121 1

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

Chapter 15 Functional Programming Languages

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

Von Neumann Architecture

Generative and accumulative recursion. What is generative recursion? Example revisited: GCD. Readings: Sections 25, 26, 27, 30, 31

YOUR NAME PLEASE: *** SOLUTIONS ***

Problem Set 2. Problem 2-1.

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

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

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

15-122: Principles of Imperative Computation, Fall 2015

Lab 3: Combinational Logic Computation Structures Spring 2019

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

An Evolution of Mathematical Tools

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

CS 215 Fundamentals of Programming II Fall 2017 Project 7. Morse Code. 30 points. Out: November 20, 2017 Due: December 4, 2017 (Monday) a n m

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

6.001: Structure and Interpretation of Computer Programs

Z Notation. June 21, 2018

DATA STRUCTURE AND ALGORITHM USING PYTHON

Part I Basic Concepts 1

NAND. Grade (10) Instructor. Logic Design 1 / 13

Boolean Representations and Combinatorial Equivalence

Assignment 4. Overview. Prof. Stewart Weiss. CSci 335 Software Design and Analysis III Assignment 4

TI-89 (and 92) Calculus Operations

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

MAS212 Assignment #1 (2018): Rational approximations

Python I. Some material adapted from Upenn cmpe391 slides and other sources

CS 360 Programming Languages Interpreters

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Common LISP-Introduction

Project 1. due date Sunday July 8, 2018, 12:00 noon

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

CSCI337 Organisation of Programming Languages LISP

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

Transcription:

CS403: Programming Languages Assignment 3 Rought Draft Preliminary information This is your third Scam assignment. To run your code, use the following command: or scam FILENAME scam -r FILENAME where FILENAME is replaced by the name of the program you wish to run. The -r option will automatically run a no-argument function named main on startup. All assignment submissions should supply a program named author.scm. This program should look like: (define (main) (println "AUTHOR: Rita Recursion rrita@crimson.ua.edu") ) with the name and email replaced by your own name and email. For each numbered task (unless otherwise directed), you are to provide a program named taskn.scm, with the N corresponding to the task number, starting at one (as in task1.scm, task2.scm, and so on). You may not use assignment, unless otherwise directed. You may not use a looping function such as while or for, unless otherwise directed. Do not use the comment-out-the rest-of-the-file comment in your code. Make sure you submit text files, not binary files. On any line of output, there should be no leading whitespace and no trailing whitespace other than a newline (except when otherwise directed). ANY ATTEMPT TO FOOL THE GRADING SCRIPT, NO MATTER HOW SMALL, WILL RESULT IN A GRADE OF ZERO WITH NO ABILITY TO RESUBMIT. This penalty can be applied retroactively at any time during or after the semester. Tasks 1. Define a predicate named scoping that determines whether a symbol is bound, free, or undefined with respect to an object. The call: (scoping 'y ((define (zurp x) (define y (+ x 1)) this) 5)) should return the symbol bound because y is a locally defined variable. The call: (scoping '* ((define (zarp x) this) 5)) should return the symbol free because * is not locally defined in the function zarp. Finally, the call: (scoping 'cs403sucks ((define (zirp x) this) 5)) should return the symbol undefined because who would define a variable like that? Use the function pptable to help you decompose scopes/objects. Do not use catch in your solution.

cat task1.args (println (scoping 'cs403sucks ((define (zirp x) this) 5))) scam -r task1.scm task1.args undefined 2. Define a function named replace that replaces all occurrences of a given symbol in a function definition with a given value. You can obtain the body of a function definition as follows: (define (square x) (* x x)) (define body (get 'code square)) (inspect body) The inspection displays the list (begin (* x x)) whose car is the symbol begin and whose cadr is the list (* x x). You can update the body of the function as follows: (set 'code '(begin (+ x x)) square) (inspect (square 5)) The inspection should display the value 10. A call to replace might look like: (replace square (list '* + '+ *)) This call would replace the times symbol with the adding built-in and the plus symbol with the multiplying built-in. You can use the replace function to speed up the execution of any function. Your replace function needs to work recursively. That is to say, it needs to replace the symbol in all local function definitions, including lambdas. You may find the predicate functions object? useful this feature as you should not descend into these kinds of objects. Neither should you process any quoted expression. You may assume the given function performs no assignments of any kind. cat task2.args (define (abs n) (if (< n 0) (- n) n)) (replace abs (list '< < '- -)) (inspect (get 'code abs)) scam -r task2.scm task2.args (get (quote code) abs) is (begin (if (<builtin <(@)> n 0) (<builtin -(@)> n) n)) You may use assignment for this task. 2

3. Define a double-ended queue class called deque. Your class should support the following methods: ((dq'enqueuefront) value) ((dq'enqueueback) value) ((dq'enqueueindex) index value) ((dq'dequeuefront)) ((dq'dequeueback)) ((dq'dequeueindex) index) ((dq'display)) (dq'size) The enqueue methods should return the item enqueued while the dequeue methods should return the item dequeued. All methods should run in constant time except enqueueindex and dequeueindex. The enqueueindex method runs in constant time when the index is a constant distance from the front and in linear time otherwise. Likewise, the dequeueindex method runs in constant time when the index is a constant distance from the back and in linear time otherwise. The enqueueindex function places the new value such that it occupies the given index, using zero-based counting. The dequeueindex also uses zero-based counting. The display function prints out the values in the dequeue, separated by commas with no added spaces. cat task3.args (define dq (deque)) ((dq'enqueuefront) 3) ((dq'enqueueback) 5) (inspect ((dq'dequeuefront))) (inspect ((dq'dequeueback))) (inspect (dq'size)) (print "-") ((dq'display)) (println "-") scam -r task3.scm task3.args ((dq (quote dequeuefront))) is 3 ((dq (quote dequeueback))) is 5 (dq (quote size)) is 0 -[]- You may use assignment for this task. 3

4. NAND is considered a universal set as any logical expression can be built solely with NAND gates. Define the constructors inverter, and-gate, or-gate, nor-gate, xor-gate, and xnor-gate, built entirely from NAND gates. Name your NAND gate constructor nand-gate. Assume a nand-gate delay of 6 time units and hard-wire this number into your nand-gate function. Use the logic gate system of the text to implement NAND and the gates built with NANDs. If you wish, you can use Scam s object system instead of the dispatch-function style of the text, which is available online. In either case, you must provide compatible make-wire, get-signal, set-signal!, make-agenda and propagate functions. The queue and other ancillary structures must be semantically equivalent as well. Use your gates to build a half-adder and a full-adder, as in the text. Place your nand-gate function by itself (with no other functions) in a file named nand.scm and include that file in your task file: You will also need to make the agenda in your task file:... (include "nand.scm")... (define the-agenda (make-agenda))... cat task4.args (define input-1 (make-wire)) (define input-2 (make-wire)) (define sum (make-wire)) (define carry (make-wire)) (probe 'sum sum) (probe 'carry carry) (half-adder input-1 input-2 sum carry) (set-signal! input-1 1) (propagate) (set-signal! input-2 1) (propagate) (println) scam -r task4.scm task4.args sum 0 New-value = 0 carry 0 New-value = 0 carry 6 New-value = 1 sum 6 New-value = 1 carry 12 New-value = 0 sum 12 New-value = 0 sum 18 New-value = 1 sum 24 New-value = 0 sum 30 New-value = 1 carry 42 New-value = 1 sum 60 New-value = 0 You may use assignment for this task. 4

5. Define a function named smush that takes a stream of the form (a, b, c, d,...) and the operator Φ and returns the stream ((0, a, a), (1, b, aφb), (2, c, aφbφc), (3, d, aφbφcφd),...), only flattened. You will also need to define a stream displaying function named sdisplay. See the example below for its behavior. cat task5.args (define ones (cons-stream 1 ones)) (sdisplay 12 (smush ones +)) (println) scam -r task5.scm task5.args (0,1,1,1,1,2,2,1,3,3,1,4,...) For every triplet, the first number, i, is the index of the second number in the original stream. The third number is the accumulation of the first 6. Define a series of functions for mathematically manipulating a quadratic equation represented as a stream. The first, quad, takes the three coefficients of the quadratic plus a step value. For example, (quad 1 3-4 0.01) would produce the stream representing the equation f = x 2 + 3x 4. The step argument, s, gives the distance between evaluations of the quadratic, starting at zero. The stream produced would be equivalent to (f(0), f(s), f(2s), f(3s),...). The second function, integrate, produces a stream representing the integral of the given stream. You should use the trapezoid method to compute the area under the curve. The first element of the integrated stream would be zero; the second element would be computed from a trapezoid having f(0) as the left height and f(s) as the right height. The integrate function takes two arguments, the stream to integrate and the step size. The i th element of the resulting stream should approximate: i s f(x)dx 0 The third function, derivate, produces a stream representing the derivative of the given stream. It should reverse the integration process. An integrated quadratic stream passed to derivate should produce the original quadratic stream. The derivate function takes two arguments, the stream to differentiate and the step size. A fourth function, same-stream? will help you decide if a differentiated integral stream is the same as the original stream. Given two streams, s1 and s2, a count n, and a positive threshold t, returns true if all of the first nth elements (piecewise) differ by less than t and false otherwise. cat task6.args (define qs (quad 1 0 0 1)) (define is (integrate qs 1)) (define ds (derivate is 1)) (sdisplay 5 qs) (println) (sdisplay 5 is) (println) (sdisplay 5 ds) (println) (inspect (same-stream? qs ds 10.001)) scam -r task6.scm task6.args (0.000000e+00,1.0000000000,4.0000000000,9.0000000000,16.000000000,...) (0.000000e+00,0.5000000000,3.0000000000,9.5000000000,22.000000000,...) (0.000000e+00,1.0000000000,4.0000000000,9.0000000000,16.000000000,...) (same-stream? qs ds 10 0.0010000000) is #t 5

7. Consider this series: 1 x2 2! + x4 4! x6 6! + x8 8!... Define a function named mystery that returns the stream that holds the terms of the above series for a given x. Define a function named ps-mystery that produces the stream of partial sums of (mystery x). Define a function named acc-mystery that accelerates (ps-mystery x) using the Euler transform. Define a function named super-mystery that produces a super accelerated stream using a tableau of ever-accelerated partial sum streams. All of these function takes x as their sole arguments. Define a function named sym-mystery that prints the symbolic result (as a LaTeX math expression) of the series sum, when taken out to infinity. For example, if the answer is π, then (mystery x) is \pi would be printed. If the answer is log π, then (mystery x) is \log\pi would be printed. In this last example, note the lack of parentheses. If there is more than one reasonable way to represent the answer, prefer the one with the fewer number of characters in the LaTeX expression. Prefer symbolic renderings over numeric ones and integers over integers rendered as real numbers. If you are unsure which rendering is best, ask me privately. Note that the dollar signs in the output are mandatory. Do not share this answer in any way. cat task7.args (define x 0) (sdisplay 5 (mystery x)) (println) (sdisplay 5 (ps-mystery x)) (println) (sdisplay 5 (acc-mystery x)) (println) (sdisplay 5 (super-mystery x)) (println) (symbolic-mystery) scam -r task7.scm task7.args (1.0000000000,-0.000000e+00,0.000000e+00,-0.000000e+00,0.000000e+00,...) (1.0000000000,1.0000000000,1.0000000000,1.0000000000,1.0000000000,...) (1.0000000000,1.0000000000,1.0000000000,1.0000000000,1.0000000000,...) (1.0000000000,1.0000000000,1.0000000000,1.0000000000,1.0000000000,...) (mystery x) is \log\pi Of course, log π is not the answer. Or is it? 8. Exercise 3.71 in the text. Define a function named ramanujan that produces a stream of Ramanujan numbers. The function takes no arguments. cat task8.args (sdisplay 1 (ramanujan)) (println) scam -r task8.scm task8.args (1729,...) Handing in the tasks For preliminary testing, send me all the files in your directory by running the command: submit proglan lusth test3 For your final submission, use the command: submit proglan lusth assign3 6