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

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

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

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.

Functional Programming. Pure Functional Programming

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

CS3 Midterm 1 Fall 2006

UNIVERSITY of CALIFORNIA at Berkeley Department of Electrical Engineering and Computer Sciences Computer Sciences Division

CS3 Midterm 2 Summer 2008

CS3 Midterm 1 Fall 2007 Standards and solutions

CS3: Introduction to Symbolic Programming. Lecture 5:

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

Programming Project #4: A Logo Interpreter Summer 2005

Chapter 11 :: Functional Languages

CS3 Fall 05 Midterm 1 Standards and Solutions

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

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

FP Foundations, Scheme

Read and fill in this page now

Functional Programming

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSC 533: Programming Languages. Spring 2015

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

An Introduction to Scheme

Chapter 1. Fundamentals of Higher Order Programming

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:

Functional Programming. Pure Functional Languages

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

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

Project 2: Scheme Interpreter

Turtles All The Way Down

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

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

(scheme-1) has lambda but NOT define

Functional Programming. Pure Functional Languages

Solutions and grading standards

CSCI337 Organisation of Programming Languages LISP

O(1) How long does a function take to run? CS61A Lecture 6

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

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

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

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

CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

Logo Features We'll first present a sample Logo program, and then discuss each component separately.

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

CS61A Notes Week 1A: Basics, order of evaluation, special forms, recursion

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

CS3 Midterm 2 Standards and Solutions

COP4020 Programming Assignment 1 - Spring 2011

CSc 520 Principles of Programming Languages

Discussion 12 The MCE (solutions)

CSCC24 Functional Programming Scheme Part 2

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

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

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

Functional Programming. Big Picture. Design of Programming Languages

Ruby: Introduction, Basics

CSc 520 Principles of Programming Languages

Administrivia. Simple data types

CSc 520 Principles of Programming Languages

Documentation for LISP in BASIC

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

4/19/2018. Chapter 11 :: Functional Languages

Lexical vs. Dynamic Scope

Introduction to Functional Programming

Programming Languages

CSE 130 [Winter 2014] Programming Languages

A brief tour of history

Organization of Programming Languages CS3200/5200N. Lecture 11

Scheme Quick Reference

MetacircularScheme! (a variant of lisp)

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Functional Languages. Hwansoo Han

Func%onal Programming in Scheme and Lisp

Lecture 7: Primitive Recursion is Turing Computable. Michael Beeson

Func%onal Programming in Scheme and Lisp

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

An Explicit-Continuation Metacircular Evaluator

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Lisp. Versions of LISP

Scheme Quick Reference

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

JavaScript Basics. The Big Picture

6.001 Notes: Section 4.1

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

CS61A Midterm 2 Review (v1.1)

ALISP interpreter in Awk

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

Environment Diagrams. Administrivia. Agenda Step by Step Environment Diagram Stuff. The Rules. Practice it s on! What are The Rules?

Ruby: Introduction, Basics

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

Chapter 15 Functional Programming Languages

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

The Very Basics of the R Interpreter

CS 314 Principles of Programming Languages. Lecture 16

BAD BAD BAD! This doesn t work! REVIEW: define the animal class. CS61A Lecture 15. dogs inherit from animals (& can call parent methods) Inheritance

A function that invokes itself is said to

6.001 Notes: Section 15.1

String Computation Program

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Ruby: Introduction, Basics

Transcription:

The Interpreter + Calling Functions 3 3 Why not 3 + 4 (+ 3 4) 7 (+ 3 4 5 6) 8 Here we were calling the function + The Interpreter + Calling Functions 4 (+ 3 (sqrt 6)) 7 (+ 3 4 5 6) 8 (sqrt 6) Not all procedures are punctuation Calling two functions Work from the inside-out Scheme was designed by people 0 (*) (- 9 5 2) 2 (+) Someone designed these to make sense. EVERYTHING should make sense! You might disagree, but you should still understand the rationale Parentheses Matter We asked scheme 9 what this is, it said: 9 9 We asked scheme what this is, it said: + a procedure #[closure arglist=args 96d20] BIG IDEA: Procedures * are a thing #[closure arglist=args 970d0] You can not include extra parentheses! Clickers Clickers are required (register online) Helps me see what concepts are challenging for the class Helps you see what concepts are challenging for you Provides Explanations from peers Experience explaining tough concepts

TRY IT Quoting stuff (+ 7(* 3 4)(* (/ 0 5)(- 3 0 How many parentheses do we need at the end? a)0 b) c)2 d)3 e)4 If you finish early calculate the answer! + + hello hello (+ 2 3) (+ 2 3) (good morning)) (good morning) These are words These are s Notice the lack of quote on the result Functions for words/s 2 74 h hello (first 274) (butfirst 274) (first hello) (first (hello)) word Sort of weird that they work on: numbers words s REVIEW: Two Types of( s so far (first (hello)) Call a function Indicate a REVIEW: Two Types of s so far http://csillustrated.berkeley.edu hi (hello) word 2

Functions for words/s e (first (butfirst hello)) (se (first 23) (last 45)) 24 Work from the inside out. 0% knowledge 90% care hi a) i TRY IT b) i c) <= this is blank d) () e) () (first (hi)) (butfirst (hi)) inst.eecs.berkeley.edu/~cs6a 3

*** Error: Undefined Variables undefined variable: Current eval stack: --------------------- 0 The word Without a quote scheme things this is a variable This shows what calls preceded the error 3.4 (+ 7) 0.4 Defining Variables (define 3.4) Make a variable Now no error! The word is still Can be different used in expressions REVIEW: Way to define variables (define variable value) Keyword & special form Shouldn t be an expression An expression ( 5) *** Error: Bad Function function eval bad function in: ( 5) Current eval stack: --------------------- 0 ( 5) (define 3.4) Make a variable This assumes that was a square (square 5) 25 Function Definition (define 3.4) (define (square x) (* x x)) Make a symbol (aka variable) Define a function (define (average x y) Keyword Function Name ( / (+ x y) 2 )) (average 4 5) Formal parameters Body Actual argument value 4

Defining plural (define (plural wd) (word wd s)) Implicitly returns last thing Predicates Predicates are procedures that return #t or #f by convention, their names end with a "?" odd? (odd? 3) #t even? (even? 3) #f vowel? (vowel? 'a) #t (vowel? (first 'fred)) #f? (? 'fred) #f Defining Plural (try 2) (define (plural wd) ) (if (equal? (last wd) y) ) (word (bl wd) ies) (word wd s) IF & COND Statements (if <predicate> <true case> <false case>) (cond (predicate return_expression) (predicate2 return_expression2) (else return_expression3)) Try It Write a better plural function using cond Works for fox/foxes and wolf/wolves (cond (predicate return_expression) (predicate2 (else return_expression2) return_expression3)) Factorial (Recursion Review) 0! = 0*9! (recursive case) x! = x*(x-)!! = (base case) 0! = (base case) (define (factorial x) (if (< x 2) (* x (factorial (- x )))) 5

What is (factorial 4)? (define (factorial x) (if (< x 2) (* x (factorial (- x )))) (factorial 4) (* 4 (factorial 3) (* 3 (factorial 2) (* 2 (factorial ) All Recursive Procedures Need. Base Case (s) Where the problem is simple enough to be solved directly 2. Recursive Cases (s). Divide the Problem (Make the problem Smaller!) into one or more smaller problems 2. Invoke the function Have it call itself recursively on each smaller part 3. Combine the solutions Combine each subpart into a solution for the whole Try It! Write count that takes in a and counts the words in the. Count the number of words in a (define (count sent) (if (empty? sent) ;no more? 0 ;base case: return 0 (+ (count (bf sent))) ;recurse on the ; rest of sent )) >(count '(a b c)) (count ( a b c )) (+ (count ( b c )) (+ (count ( c )) (+ (count ()) 0 Try It! Write coes that takes in a word and a variable n and repeats the word n times in a. Coes (define (coes n wd) (if (< n ) () ( wd (coes (- n ) wd)))) (coes 4 ha) (se ha (se ha (se ha (coes 3 ha) (coes 2 ha) (coes ha) (se ha (coes 0 ha) () 6