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.

Similar documents
Read and fill in this page now. Your lab section day and time: Name of the person sitting to your left: Name of the person sitting to your right:

CS3 Midterm 1 Fall 2006

Problem 1. Remove consecutive duplicates (6 points, 11 mintues)

CS3 Fall 05 Midterm 1 Standards and Solutions

Read and fill in this page now. Your instructional login (e.g., cs3-ab): Your lab section days and time: Name of the person sitting to your left:

Read and fill in this page now

CS 3 Midterm 1 Review

CS3 Midterm 1 Fall 2007 Standards and solutions


CS3 Midterm 2 Standards and Solutions

Department of Electrical Engineering and Computer Sciences Spring 2001 Instructor: Dan Garcia CS 3 Midterm #2. Personal Information

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

CS3: Introduction to Symbolic Programming. Lecture 8: Introduction to Higher Order Functions. Spring 2008 Nate Titterton

CS3 Midterm 2 Summer 2008

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

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

CS 3L (Clancy) Solutions and grading standards for exam 1

We've tried to include the common errors and grading standard for every question.

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

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

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

6.001 Notes: Section 8.1

Solutions and grading standards

Lesson 4: Who Goes There?

In our first lecture on sets and set theory, we introduced a bunch of new symbols and terminology.

PROFESSOR: Last time, we took a look at an explicit control evaluator for Lisp, and that bridged the gap between

CS3: Introduction to Symbolic Programming. Lecture 5:

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

CS 61B, Midterm #3 and solutions, Spring 1996

CSE341 Spring 2017, Final Examination June 8, 2017

These are notes for the third lecture; if statements and loops.

6.001 Notes: Section 15.1

University of California, Berkeley College of Engineering

CS3: Introduction to Symbolic Programming. Lecture 10: Tic-tac-toe Lambda.

CS61B (Clancy) Spring 1996 Exam 3, solutions, and grading standards. April 12, 1996 Exam 3

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

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

Drawing Hands, by M. C. Escher (lithograph, 1948)

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

Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis. Copyright 2012 L. Leona Davis All Rights Reserved

MITOCW watch?v=zm5mw5nkzjg

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

CSE341 Spring 2017, Final Examination June 8, 2017

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5


CPS 310 midterm exam #1, 2/19/2016

YOUR NAME PLEASE: *** SOLUTIONS ***

CS 303E Fall 2011 Exam 2 Solutions and Criteria November 2, Good Luck!

The Design Process. General Development Issues. C/C++ and OO Rules of Thumb. Home

Account joeacct = new Account (100, new Account (500)); Account joeacct = new Account (100, new Account (500, null));

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #06 Loops: Operators

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

(Refer Slide Time 3:31)

CS3: Introduction to Symbolic Programming. Lecture 11: Tree Recursion, beginning lists, and Midterm 2. Spring 2007 Nate Titterton

CS3: Introduction to Symbolic Programming. Lecture 14: Lists Scheme vs. other programming languages.

6.001 Notes: Section 6.1

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

CS103 Handout 50 Fall 2018 November 30, 2018 Problem Set 9

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

MITOCW watch?v=0jljzrnhwoi

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

Chapter 10 Recursion

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

CS 455 Midterm Exam 2 Fall 2015 [Bono] Nov. 10, 2015

CSE200 Lecture 6: RECURSION

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

University of Massachusetts Lowell

Create your first workbook

Understanding Browsers

CS61A, Fall/1999 Midterm #1 Professor Brian Harvey

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

MITOCW watch?v=flgjisf3l78

If Statements, For Loops, Functions

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

A lot of people make repeated mistakes of not calling their functions and getting errors. Make sure you're calling your functions.

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

The compiler is spewing error messages.

Programming Principles 1 (CSC131) & 2 (CSC132) Software usage guide

MITOCW watch?v=rvrkt-jxvko

BEGINNER PHP Table of Contents

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

Raft and Paxos Exam Rubric

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

COMP-202 Unit 0: Course Details

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

Algebraic Specifications

Project 5 - The Meta-Circular Evaluator

CS1102: Adding Error Checking to Macros

Formal Methods of Software Design, Eric Hehner, segment 24 page 1 out of 5

CS8 Final Exam E03, 09M, Phill Conrad, UC Santa Barbara 09/10/2009

The following content is provided under a Creative Commons license. Your support

Types, Expressions, and States

(first (hello)) (hello) CS61A Lecture 2. Computer Science. Hierarchy of Abstraction. Functions. REVIEW: Two Types of( s so far

This exam is worth 30 points, or about 28% of your total course grade. The exam contains

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

AHHHHHHH!!!! NOT TESTING! Anything but testing! Beat me, whip me, send me to Detroit, but don t make me write tests!

(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

CS3L Summer 2011 Exam 2 Time: up to 170 minutes (you may leave when finished; or, you must stop promptly at noon)

Transcription:

CS3 Fall 04 Midterm 1 Solutions and Grading standards Problem 1 (6 points, 10 minutes): Make the expressions correct Add parentheses and procedures in the underlined areas to make these expressions return the value specified. Do not define any new procedures. Be careful with parentheses and quotes! You can leave the underlined areas blank, but all the explicit text must be included in your expression you can't just leave something out. If it is impossible to create an expression that returns the value specified, write IMPOSSIBLE somewhere (to the right of the return value, say). 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. 1] 2] 3] 4] 5] 6] 7] ( bl ( bl (se 'scheme 'is 'a 'state 'of 'mind ) ) ) (scheme is a state) ( sentence a (item 3 '(state of mind) )) (a mind) ( item 8 (first '(expression) ) ) i ( word (first '(go cal bears) ) 'cal 'bears ) gocalbears (count "" ) 0 (if (or #f (and #f)) 'yep 'nope) yep IMPOSSIBLE (define (what-the sent) (if (empty? sent) "" (word "-um-" (first sent) (what-the (bf sent))))) (what-the '( I like "" cheese ) ) "-um-i-um-like-um--um-cheese" Part [5] also works with '(), which many of you used. Some of you came up with other solutions for part [7] that none of us thought of, e.g.: (what the '(I like um-cheese)) Each of these problems was worth one point, but only six points were possible. Each problem was graded all-or-nothing (i.e., no partial credit). - 1 -

Problem 2 (5 points, 10 minutes) : Validating dates for day-span Write a procedure called day-span-validated which takes the same arguments as day-span and returns the same value as day-span when the argument dates are valid (i.e., legal). If either of the dates is invalid, however, day-span-validated should return (invalid date). For instance: (day-span-validated '(february 30) '(june 3)) (day-span-validated '(june 4) '(may 15)) (day-span-validated '(january 19) '(flugalmonth 5)) (day-span-validated '(january three) '(may five)) (invalid date) -19 (invalid date) (invalid date) You can assume that each argument to day-span-validated is a sentence containing two words. You can (and should) use procedures from the Difference Between Dates case study, part I, that will help you. This code is included in an appendix at the end of the exam. In particular, consider the procedures day-span, days-in-month, month-name, and date-in-month. Assume that this is not a leap year. There were three basic things you had to do for this problem: 1) make sure the dates are correct 2) call day-span correctly, if the dates are valid 3) correctly return (invalid date) if the dates are invalid 4) use procedures from the Difference Between Dates case study Parts 2 and 3 are pretty simple. We took off 1 point each if they were missing. Part 4 requires that you use date-in-month instead of last when you want the day of the month, month-name instead of first if you want the name of the month, days-in-month if you want to know how many days are in a month, and day-span if you want to do day-span. If you missed some of this, you lost a point. Many people wrote something like: ((and (member? (month-name date) '(september april june november)) (> (date-in-month date) 30)) #f) and so on for 31-day months and again for February. You could and should have done this with one line: ((> (date-in-month date) (days-in-month (month-name date))) #f) Part 1 is more complicated. There are at least four things you should do. 1) Make sure the name of the month is valid 2) Make sure the day is a number 3) Make sure the day is > 0 4) make sure the day is < (days-in-month (month-name date)) - 2 -

If you missed one of these, you lost a point. If you checked to see if the day was a number only after you compared it to other numbers, you lost half a point. You can't do < or > on something that's not a number, after all. Several people really messed up their conds or ifs. Every if needs three parts: a test, something you do if the test returns true, and something you do if the test returns false. Putting a bunch of ifs together in a column will not make them work like a cond. Mistakes like these were worth between 1 and 2 points, depending on how confused the ifs were. There were several cond mistakes. First, some people did something like (cond ((and (valid-month? date1)(valid-month? date2)) ((and (number? (date-in-month date1)) (number? (date-in-month date2))) ((and (> (date-in-month date1) 0) (> (date-in-month date2))) ((and (<= (date-in-month date1) (days-in-month (month-name date1)) (<= (date-in-month date1) (days-in-month (month-name date1))) (else '(invalid date))) This will call day-span if the dates pass any one of those four tests. Making this mistake cost people two points. Another mistake was to leave off one set of parentheses from every cond line: (cond (not (and (valid-month? date1) (valid-month? date2))) #f... This mistake cost people 1.5 points. - 3 -

Problem 3 (6 points, 10 minutes): Data Abstraction in School You are part of a team working on a project to create, among other things, a database system for schools. One of your tasks is to implement the school datatype in Scheme. This question does not require recursion. In the project, a school will have three pieces of information: A short name, which will be a single word, A full name, which will be one or more words, Enrollment, a number For instance, here are some possible schools: full name short name enrollment University of California Berkeley Cal 23000 Rice Rice 2822 California Institute of Technology Caltech 900 This problem confused many people, but you have seen constructors and selectors in lab, lecture, and the book (look it up!). The Scheme syntax of the solutions was hardly difficult, which may have thrown some of you. The concept of data abstraction is a difficult one, but something important for this course! Part A Make a constructor procedure for the school datatype. Choose a reasonable name for the procedure, and include good comments. (You will, of course, need to decide how the data is stored; there is more than one correct way to do this.) Include a call to your constructor. A solution: (define (make-school fname sname enroll) (se sname enroll fname)) (make-school '(university of california Berkeley) 'cal 23000) (cal 23000 university of california berkeley Other orderings of arguments and packaging up of the sentence were possible. Packaging up the three pieces of information into a word, while technically possible, would have required recursion in the selectors. You lost 0.5 points for forgetting to provide a call; 0.5 points for a poor name. Bad or missing comments lost no points. Using selectors or including wrong arguments lost between 1 and 2 points, depending on the rest of the logic. Part B Make the three selector (accessor) procedures, with reasonable names and comments. DO NOT use recursion when writing these. - 4 -

Using the above constructor: (define (full-name sch) (bf (bf sch)) ) (define (short-name sch) (first sch) ) (define (enrollment sch) (first (bf sch)) ) Not writing selectors consistent with your constructor lost 0.5 points each (or 1 point from the constructor, depending). Other problems were worth between 0.5 to 1 point for each selector. Part C What is a reasonable name of the boolean test procedure for this datatype? (That is, the procedure that somebody can use to test whether a Scheme value is a school or not.) Don't write this procedure, just name it. The best name is school?, as with other predicates. Not having the question mark lost 1 point; a name that didn't imply "is this a school" lost 1 point [the majority of you got this question]. Problem 4 (5 points, 9 minutes): Argue Write a function called argue-response which takes a sentence and returns a sentence in which the meaning is reversed. Perhaps the easiest way to explain how it reverses meaning is through some examples: (argue-response '(I hate the opera)) (you love the opera) (argue-response '(you love cookies)) (I hate cookies) (argue-response '(you hate (I love everything that everything that I cook)) you cook) If the first two words in the sentence are not of the form you've seen, argueresponse takes a different tack, adding (all you ever say is) to the input: (argue-response '(lets have dinner)) (argue-response '(what)) (all you ever say is lets have dinner) (all you ever say is what) The third example in the first set of test cases above contained an unfortunate error: the second to last "I" in the argument was transformed to a "you" by argue-response. Only a few of you caught this: solving for it required recursion. You were not taken off for not doing this recursion; including a recursive solution gave you some leeway to miss a point elsewhere (which no one did). - 5 -

Part A: Fill in the blanks below, assuming that the code implied by [ ] is written correctly. (define (argue-response remark) (cond ((starts-with? remark '(i hate)) ( se 'you 'love ( bl ( bl remark))) ) ((starts-with? remark '(i love)) [ ] ) ((starts-with? remark '(you love)) [ ] ) ((starts-with? remark '(you hate)) [ ] ) (else ) ) ) (se '(all you ever say is) remark) You received 1 pt for each blank filled in correctly. We took off _ a point if it was correct, but had something minor wrong with it (such as an extra set of parentheses). If a recursive solution was attempted (due to the typo on the midterm), then we gave full credit if it was anything reasonable. Including [ ] in your answer was not correct! Part B: Write the function starts-with? so that the above code works correctly. One solution (non-recursive): (define (starts-with? remark first-two) (and (equal? (first remark) (first first-two)) (equal? (first (bf remark)) (first (bf first-two))) )) You received 1 pt each for the following: - Pulling out the first two words of the remark (for the purpose of some kind of comparison). - Actually doing a correct comparison involving the remark and the other two word sentence given as an argument. Typically to get this point, you needed to actually have starts-with? defined to take two arguments. - Putting these two pieces together correctly and returning a boolean value _ pt was taken off for minor infractions, such as having extra parentheses - 6 -

Problem 5 (5 points, 10 minutes): Count-vowels Write a recursive procedure named count-vowels which takes a single word as input, and returns the number of vowels in the word. Assume that the function vowel? has been defined. You may define and use any other helper functions that make sense to you. For instance, (count-vowels 'fred) 1 (count-vowels 'frederick) 3 (count-vowels 'happy) 1 This question seemed reasonable for most of you: a recursion down a word in which the recursive case conditionally added 1 to the recursive call, and the base case was a number. A solution with ifs: (define (count-vowels wd) (if (empty? wd) 0 (if (vowel? (first wd)) (+ 1 (count-vowels (bf wd))) (count-vowels (bf wd))) )) The correct base case was worth 1 point total: 0.5 for getting the test right, 0.5 for getting the return value right. Handling each side of the condition in the recursive case (i.e., when (vowel? wd) returned true or false) was worth 2 points. - 7 -