CS1 Recitation. Week 1

Similar documents
Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

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

Numerical Data. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

Computer Science 21b: Structure and Interpretation of Computer Programs (Spring Term, 2004)

Ch. 12: Operator Overloading

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

CSE 341: Programming Languages

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

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Variables, expressions and statements

Mark Important Points in Margin. Significant Figures. Determine which digits in a number are significant.

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

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

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

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

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

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS Introduction to Data Structures How to Parse Arithmetic Expressions

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

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

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

Expressions and Casting

Class Structure. Prerequisites

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

Arithmetic Expressions in C

Documentation for LISP in BASIC

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

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Midterms Save the Dates!

Part 2,Number Systems Questions

Midterms Save the Dates!

Midterms Save the Dates!

SCHEME AND CALCULATOR 5b

Learning the Language - V

Rev Name Date. . Round-off error is the answer to the question How wrong is the rounded answer?

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

Fundamentals and lambda calculus

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Modesto City Schools. Secondary Math I. Module 1 Extra Help & Examples. Compiled by: Rubalcava, Christina

WELCOME! (download slides and.py files and follow along!) LECTURE 1

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

CS 135 Lab Assignments Week 1

Func+on applica+ons (calls, invoca+ons)

Welcome to CS 115 (Winter 2018)

Chapter 1 Section 1 Lesson: Solving Linear Equations

Intro to Haskell Notes: Part 5

Encodings. Using the minimal λ-calculus language we get. functions. local binding. booleans. numbers... and recursive functions?

An Explicit Continuation Evaluator for Scheme

1/11/2010 Topic 2: Introduction to Programming 1 1

Welcome to CS 115 (Winter 2019)

Unit 3. Constants and Expressions

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Values and Variables 1 / 30

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Fundamentals and lambda calculus. Deian Stefan (adopted from my & Edward Yang s CSE242 slides)

Topic 2: Introduction to Programming

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

I can solve simultaneous equations algebraically and graphically. I can solve inequalities algebraically and graphically.

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

There are two ways to use the python interpreter: interactive mode and script mode. (a) open a terminal shell (terminal emulator in Applications Menu)

>>> * *(25**0.16) *10*(25**0.16)

EXPRESSIONS AND ASSIGNMENT CITS1001

,!7IA3C1-cjfcei!:t;K;k;K;k ISBN Graphing Calculator Reference Card. Addison-Wesley s. Basics. Created in conjuction with

CS102: Variables and Expressions

Chapter 2: Using Data

Making Atlas2 maps for TGEA. Part 0: Notes on Scripting in TGEA. Part 1: Converting the.raw file into a geometry chunk file

COMP 110 Introduction to Programming. What did we discuss?

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

CS1 Recitation. Week 2

Debugging Your Python Code: For Dummies

CS11 Java. Fall Lecture 1

Week 6: Processor Components

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Language Reference Manual simplicity

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Chapter 17. Fundamental Concepts Expressed in JavaScript

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Admin CS41B MACHINE. Midterm topics. Admin 2/11/16. Midterm next Thursday in-class (2/18) SML. recursion. math. David Kauchak CS 52 Spring 2016

Warm-up and Memoization

5.7 Solving Linear Inequalities

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

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

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

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

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

More about Binary 9/6/2016

Transcription:

CS1 Recitation Week 1

Admin READ YOUR CS ACCOUNT E-MAIL!!! Important announcements, like when the cluster will be unavailable, or when you need to reset your password. If you want to forward your e-mail: Make a.forward file in your home directory Put your preferred e-mail address in there E-mail will be forwarded (no copy will be kept on CS cluster)

Converting Math to Scheme 4 + 9 * 3 =??? 31, not 39. The * is applied first, then the + * has higher precedence than + Scheme doesn t have precedence rules All combinations are explicitly wrapped in parentheses When converting math expressions to Scheme, do highest precedence operations first (* 9 3) (+ 4 (* 9 3))

More Complicated Example 3 + 15 / 4 + 6 * 9 / and * have the highest precedence (+ 3 (/ 15 4) (* 6 9)) Could also write: (+ (+ 3 (/ 15 4)) (* 6 9)) Arithmetic operators can take more than two arguments Simplify expressions, if possible. Make sure simplified expression still accurately represents original (i.e. don t flip signs, do reciprocals, etc.)

Evaluating Scheme Combinations Evaluating combinations is very simple. 1. Evaluate operands Scheme doesn t specify an order for operands. Left-to-right is simple and clear, so do that. 2. Evaluate the operator Don t forget this step! In CS1, we do this after evaluating operands 3. Apply operator to operands 4. State result of computation

Simple Example (+ 4 (* 9 3)) Evaluate 4 4 Evaluate (* 9 3) Evaluate 9 9 Evaluate 3 3 Evaluate * [primitive procedure *] Apply * to 9, 3 27 Result: 27 Evaluate + [primitive procedure +] Apply + to 4, 27 31 Result: 31

Numbers in Scheme What do you get when you run (/ 3 9)? 1/3, not 0.333 1/3 is an exact answer, 0.333 is not Scheme preserves exactness, if it can Math will result in fractions by default, unless you use decimal in the operands. (/ 3 9) = 1/3 (/ 3.0 9) = 0.333 (/ 3 9.0) = 0.333 (/ 3.0 9.0) = 0.333

Definitions in Scheme (define x 53) Remember define is a special form! Skip first operand, evaluate second operand. 53 53 Associate 53 with x (can also say Bind 53 to x ) Don t forget to evaluate define statements State that it s a special form, so that it s clear why second operand is evaluated first.

Lambda Expressions (lambda (x) (* x x)) lambda creates a procedure in Scheme Evaluates to itself: (lambda (x) (* x x)) (lambda (x) (* x x)) Or: (lambda (x) (* x x)) itself Can be combined with define: (define sq (lambda (x) (* x x))) Now can use sq as a function: (sq 5) produces 25

Evaluating Defines with Lambdas Evaluate (define sq (lambda (x) (* x x))) define is a special form. Skip first operand. Evaluate second operand. Evaluate (lambda (x) (* x x)) itself Associate the new function with the name sq

Simplified Function Definitions Can also say: (define (sq x) (* x x)) This is syntactic sugar for: (define sq (lambda (x) (* x x))) When evaluating the sugared form, you must explicitly state the desugaring step. Evaluate (define (sq x) (* x x)) Desugar to: (define sq (lambda (x) (* x x))) Define is a special form. Evaluate second operand. Continue the evaluation of define as usual

Using Our New Function Evaluate (sq 5) Evaluate 5 5 Evaluate sq (lambda (x) (* x x)) Apply (lambda (x) (* x x)) to 5 Substitute 5 for x in (* x x) (* 5 5) Evaluate (* 5 5) Evaluate 5 5 Evaluate 5 5 Evaluate * [primitive procedure *] Apply * to 5, 5 25 Result: 25 Result: 25 Result: 25

What s Wrong Here? You type: 15-6 Is this valid Scheme? No! Needs to be a combination surrounded with parentheses This is a syntax error it doesn t have the right form.

What s Wrong Here? Now you type (15-6) Is this valid Scheme? What is the problem? Operator needs to be first in Scheme, then operands What does the Scheme interpreter say? 15 isn t a procedure Interpreter feedback is very limited. It can t tell you what you meant, only what it expects.

What s Wrong Here? You type (- * 6 3 4) Is this valid Scheme? It s a valid combination Functions can be operands in Scheme. However, evaluation does result in an error. Can t subtract a number from a function This is a semantic error, not a syntax error. Expression has correct form, but doesn t make sense.

Writing Useful Procedures Want to write a Scheme procedure to convert miles to kilometers. 1.6 km in a mile easy conversion! Give your procedures understandable names Bad names: m2k, convert, f, answer5b Good name: miles-to-km Document your procedures with comments! ;; Converts a distance in miles, to kilometers. (define miles-to-km )

Converting Miles to Kilometers Can write it the sugar-free way (define miles-to-km (lambda (miles) (* 1.6 miles))) Or, the sugared way (define (miles-to-km miles) (* 1.6 miles)) Use meaningful variable names too. Really important for maintainable programs Test your code! Can use google.com to check your answers 15 miles in km 15 miles = 24.14016 kilometers

Reuse Your Work! The best programmers are fundamentally lazy. Don t do unnecessary work. (Constructive laziness.) Now you want to convert furlongs to kilometers 8 furlongs = 1 mile (define (furlongs-to-km f) (miles-to-km (/ f 8))) Better yet, write a separate furlongs-to-miles fn. Then furlongs-to-km can just compose the two (define (furlongs-to-miles f) (/ f 8)) (define (furlongs-to-km f) (miles-to-km (furlongs-to-miles f)))