Guerrilla Section 7: Macros, SQL SQL

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

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

Fall 2017 December 4, Scheme. Instructions

Fall 2017 December 4, 2017

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

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

Problem Set 4: Streams and Lazy Evaluation

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

Guerrilla Section 5: Object Oriented Programming, Nonlocal & Mutable Trees

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

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

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

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

SQL AND FINAL REVIEW

From Syntactic Sugar to the Syntactic Meth Lab:

Structure and Interpretation of Computer Programs

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

Structure and Interpretation of Computer Programs

Functional Programming. Pure Functional Programming

Sample Final Exam Questions

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 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.

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

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Structure and Interpretation of Computer Programs

CS 275 Name Final Exam Solutions December 16, 2016

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CONCEPTS OF PROGRAMMING LANGUAGES Solutions for Mid-Term Examination

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

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

University of California, Berkeley College of Engineering

Building a system for symbolic differentiation

Structure and Interpretation of Computer Programs

CSE341 Autumn 2017, Final Examination December 12, 2017

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

CS1102: Macros and Recursion

Functional Programming - 2. Higher Order Functions

CSE341 Spring 2016, Final Examination June 6, 2016

CSE341 Spring 2017, Final Examination June 8, 2017

April 2 to April 4, 2018

An Explicit Continuation Evaluator for Scheme

Quicksort. Alternative Strategies for Dividing Lists. Fundamentals of Computer Science I (CS F)

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

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

User-defined Functions. Conditional Expressions in Scheme

An introduction to Scheme

CSc 520 Principles of Programming Languages

CS 314 Principles of Programming Languages. Lecture 16

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

SCHEME AND CALCULATOR 5b

Structure and Interpretation of Computer Programs

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

CSE413 Midterm. Question Max Points Total 100

TAIL RECURSION, SCOPE, AND PROJECT 4 11

Repetition Through Recursion

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

Hints for Exercise 4: Recursion

Department of Electrical Engineering and Computer Sciences Fall 2000 Instructor: Dan Garcia CS 3 Final Exam

Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

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

CS 314 Principles of Programming Languages

A Brief Introduction to Scheme (II)

CSc 520 Principles of Programming Languages

Recursion & Iteration

Building a system for symbolic differentiation

Structure and Interpretation of Computer Programs

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

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 415 Midterm Exam Fall 2003

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

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

CSCC24 Functional Programming Scheme Part 2

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

CSE341 Spring 2017, Final Examination June 8, 2017

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

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

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

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

Structure and Interpretation of Computer Programs

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

C311 Lab #3 Representation Independence: Representation Independent Interpreters

by the evening of Tuesday, Feb 6

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

Functional Programming

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

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

61A LECTURE 22 TAIL CALLS, ITERATORS. Steven Tang and Eric Tzeng July 30, 2013

Lexical vs. Dynamic Scope

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

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Lecture 35: Analysis Review & Tail Recursion 10:00 AM, Nov 28, 2018

CSE341, Spring 2013, Final Examination June 13, 2013

CS 314 Principles of Programming Languages

Structure and Interpretation of Computer Programs

Streams and Evalutation Strategies

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

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Project 2: Scheme Interpreter

CS 314 Principles of Programming Languages

Discussion 4. Data Abstraction and Sequences

Transcription:

Guerrilla Section 7: Macros, SQL Instructions Form a group of 3-4. Start on Question 1. Check off with a staff member when everyone in your group understands how to solve the questions up to the first checkpoint. Repeat for the second checkpoint, the third checkpoint, and so on. You're not allowed to move on after a checkpoint until you check off with a staff member. You are allowed to use any and all resources at your disposal, including the interpreter, lecture notes and slides, discussion notes, and labs. You may consult the staff members, but only after you have asked everyone else in your group. The purpose of this section is to have all the students working together to learn the material. SQL We will be working with a Facebook-like website called Fakebook. The data we will be using will be in fakebook.sql (Google Drive link). Load it in your interpreter with sqlite>.read fakebook.sql OR, if you don t have sqlite3 installed, you can use an online SQL interpreter to test your solutions. If you re using sqlite3, edit your queries in some text editor (e.g. Sublime) and read them in so you can easily change them. For example, the query SELECT * FROM people will allow you to view all contents of the people table. There are four tables in the provided Fakebook data, summarized below: Table Name and Columns people(name, age, state, hobby) posts(post_id, poster, text, time) likes(post_id, name, time) requests(friend1, friend2) Table Information Description: Each row represents... a person on Fakebook a post with its creator and creation time (in minutes, starting at 0) a like: post_id of the post that was liked, name of person who liked the post, and time (in minutes) of like a friend request from friend1 to friend2 The first two rows of the people table: name age state hobby Carolyn 52 Florida karaoke

Dan 47 Maine disc golf The first two rows of the posts table: post_id poster text time 1 Mike Scorpions 104 2 Jenn Jetskis 124 The first two rows of the likes table: post_id name time 1 Kelly 105 1 Mike 108 The first two rows of the requests table: friend1 Carolyn Carolyn friend2 Joaquin Kelly Question 1: Fill in the blanks! (Part I) Fill in the table below with the query that would produce the expected output Desired Information Expected Output Query The name and age of each person on Fakebook who is 26 years old or younger The name of the poster and the time of each post on Fakebook before minute 230 Hali 25 Jenn 22 Joe 25 Lindsey 24 Rodney 24 Mike 104 Jenn 124 So 134 Nina 229

The names of users who have liked their own post Mike Vince Jenn Mike Shirin Vince Rodney Max Rodney Mike Will Question 2: Friend Requests The requests table stores all requests from one person to another. Two people are only friends if both people requested to be friends with the other. Create a table friends that has two columns (friend1 and friend2) that contains the names of each friend pairing. For example, if Hali sends a friend request to Joe and Joe sends a friend request to Hali, both Joe Hali and Hali Joe should appear in the table. CREATE TABLE friends AS SELECT FROM WHERE If you have created the table correctly, the sample query below should work. > select * from friends where friend1 = "Hali"; Hali Jenn Hali Joe Hali Shirin STOP! Don t proceed until everyone in your group has finished and understands all exercises in this section, and you have gotten checked off for Check-in #1

Question 3: Write More Queries! Recall that the aggregate functions MAX, MIN, COUNT, and SUM return the maximum, minimum, number, and sum of the values in a column. The GROUP BY clause of a select statement is used to partition rows into groups. Desired Information Expected Output Query all names of people who have at least 4 friends the states that Will's friends live in, and how many friends in each state Text from every post that was liked within 2 minutes of post time Carolyn Kelly Mike Tyler Will Arizona 1 California 2 Massachusetts 1 Texas 1 Scorpions Winner winner chicken dinner Snickers Sandwiches Every pair of people that share the same hobby, as well as that shared hobby. Make sure your output doesn t have duplicate pairs The counts of the number of people that live in each state, with each state listed in descending order of count Carolyn Will karaoke Dan Mike disc golf Hali Jenn surfing Joaquin Shirin traveling Joaquin So traveling Kelly Tyler football Shirin So traveling California 9 Florida 2 New York 2 Arizona 1 Maine 1 Massachusetts 1 Texas 1 Utah 1

Question 4: Mutation! Insert stuff! Update stuff! Delete stuff! Directions Query Send a friend request by inserting a new friend request from Denero to Hilfy Help fakebook user Denero send a friend request to every person who liked post 349 by inserting into requests Change the hobby of every person whose name is Joe to CS Create a table num_likes with the columns name, post_id, number. Each row should contain a poster s name, a post_id, and number of likes the post received Carolyn is a bit shy. Delete all of her posts in the num_likes table with fewer than 4 likes Create an empty table called privacy with columns name and visibility which should hold the default to everyone.

Add Hermish to the privacy table using the default value. STOP! Don t proceed until everyone in your group has finished and understands all exercises in this section, and you have gotten checked off for Check-in #2 Macros Question 0 What will Scheme output? If you think it errors, write Error. scm> (define-macro (doierror) (/ 1 0)) scm>(doierror) scm> (define x 5) scm> (define-macro (evaller y) (list (list 'lambda '(x) x)) y) scm> (evaller 2) Question 1 Implement stream-cons and stream-cdr based on the doctests below. You do not need to worry about multiple evaluations; in other words, stream-cdr may cause the value to be recomputed (unlike actual streams which the cdr can only be forced / evaluated once). In your implementation, you may not use cons-stream or cdr-stream. Hint : In most cases, e.g. with expressions like (define x (/ 1 0)), we evaluate an entire expression immediately, violating the properties of lazy evaluation that a stream uses. But in certain cases, we can prevent the evaluation of an expression until later. What s an example of that, and how can we use that here? scm> (define (naturals-from n) (stream-cons n (naturals-from (+ n 1)))) naturals-from scm> (define naturals (naturals-from 0)) naturals scm> (car (stream-cdr (stream-cdr (stream-cdr (stream-cdr naturals))))) 4

(define-macro (stream-cons x xs) ) (define (stream-cdr xs) ) Question 2 The goal of this question is to define a macro that represents a while loop. Since this is a difficult task we will break it into parts. 2a Write tail-recursive factorial: (define (fact n) ) 2b Using the above problem to assist implementation, create the while macro. This macro will accept 4 arguments: - initial-bindings: this will represent initialization values for variables in the loop - condition: this will represent the condition which the while loop should continue to check to see if the loop should continue - return: after the loop has ended this represents the value that should be returned You may find the built-in map function useful for this problem: scm > (map (lambda (x) (* 2 x)) (1 2 3)) (2 4 6)

And here s an example of the while macro being used to calculate the factorial: scm > (define (fact n) (while ((acc 1) (n n)) (> n 0) ((* acc n) (- n 1)) acc)) fact scm> (fact 4) 24 Fill in the following macro definition: (define-macro (while initial-bindings condition updates return) (define helper-vars ) (define initial-vals ) (list 'begin (list 'define (cons 'helper ) `(if ) )) CONGRATULATIONS! You made it to the end of the worksheet! Great work.