Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

Similar documents
Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

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

Introduction to Scheme

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

Functional Programming. Pure Functional Programming

Recursive List Func.ons in Racket

LISP: LISt Processing

Func%onal Programming in Scheme and Lisp

Lists in Lisp and Scheme

Func%onal Programming in Scheme and Lisp

A Brief Introduction to Scheme (II)

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

Scheme Quick Reference

Scheme Quick Reference

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Project 2: Scheme Interpreter

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Building a system for symbolic differentiation

Building a system for symbolic differentiation

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

YOUR NAME PLEASE: *** SOLUTIONS ***

CSc 520 Principles of Programming Languages

A Quick Introduction to Common Lisp

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming Languages

Ways to implement a language

CS 314 Principles of Programming Languages. Lecture 16

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

Discussion 4. Data Abstraction and Sequences

Essentials of Programming Languages Language

Essentials of Programming Languages Language

An Explicit-Continuation Metacircular Evaluator

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

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

6.001 recitation 3/21/07

CS 314 Principles of Programming Languages

CSC 533: Programming Languages. Spring 2015

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

ALISP interpreter in Awk

Functional Programming. Pure Functional Languages

Administrivia. Simple data types

CS 314 Principles of Programming Languages

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

LECTURE 16. Functional Programming

CS61A Summer 2010 George Wang, Jonathan Kotker, Seshadri Mahalingam, Eric Tzeng, Steven Tang

6.001 Notes: Section 8.1

Functional Programming

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

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

University of Massachusetts Lowell

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

PostFix. PostFix command semanccs (except exec) PostFix Syntax. A PostFix Interpreter in Racket. CS251 Programming Languages Fall 2017, Lyn Turbak

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

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

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

Turtles All The Way Down

CS 360 Programming Languages Interpreters

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

CSc 520. Principles of Programming Languages 7: Scheme List Processing

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

How to Design Programs Languages

Lisp Basic Example Test Questions

CS61A Midterm 2 Review (v1.1)

The Typed Racket Guide

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

CS 342 Lecture 6 Scheme Procedures and Closures By: Hridesh Rajan

Scheme: Expressions & Procedures

Scheme: Strings Scheme: I/O

(scheme-1) has lambda but NOT define

PostFix. PostFix command semanccs (except exec) PostFix Syntax. A PostFix Interpreter in Racket. CS251 Programming Languages Spring 2018, Lyn Turbak

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

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

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

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

Simplifying Logical Formulas

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

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


Documentation for LISP in BASIC

Sample Final Exam Questions

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

CSE413 Midterm. Question Max Points Total 100

Functional Programming. Pure Functional Languages

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

6.001: Structure and Interpretation of Computer Programs

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

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

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

An Introduction to Scheme

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

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

Discussion 12 The MCE (solutions)

Structure and Interpretation of Computer Programs

Typed Racket: Racket with Static Types

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

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

Transcription:

The Pros of cons: in Racket CS251 Programming Languages Fall 2018, Lyn Turbak Department of Computer Science Wellesley College booleans: #t, #f numbers: integers: 42, 0, -273 ra3onals: 2/3, -251/17 floa3ng point (including scien3fic nota3on): 98.6, -6.125, 3.141592653589793, 6.023e23 complex: 3+2i, 17-23i, 4.5-1.4142i Note: some are exact, the rest are inexact. See docs. strings: "cat", "CS251", "αβγ", "To be\nor not\nto be" characters: #\a, #\A, #\5, #\space, #\tab, #\newline anonymous func3ons: (lambda (a b) (+ a (* b c))) What about compound data? Racket Values 2 cons Glues Two Values into a Pair A new kind of value: pairs (a.k.a. cons cells): (cons V1 V2) e.g., - (cons 17 42) - (cons 3.14159 #t) In Racket, type Command-\ to get λ char - (cons "CS251" (λ (x) (* 2 x)) - (cons (cons 3 4.5) (cons #f #\a)) Can glue any number of values into a cons tree! Box-and-pointer diagrams for cons trees (cons v1 v2) v1 v2 Conven3on: put small values (numbers, booleans, characters) inside a box, and draw a pointers to large values (func3ons, strings, pairs) outside a box. (cons (cons 17 (cons "cat" #\a)) (cons #t (λ (x) (* 2 x)))) 17 #\a #t (λ (x) (* 2 x)) 3 "cat" 4

Big step seman,cs: Evalua3on Rules for cons E1 V1 E2 V2 (cons E1 E2) (cons V1 V2) [cons] Small-step seman,cs: cons has no special evalua3on rules. Its two operands are evaluated le\-to-right un3l a value (cons V1 V2) is reached: cons evalua3on example (cons (cons {(+ 1 2)} (< 3 4)) (cons (> 5 6) (* 7 8))) (cons (cons 3 {(< 3 4)}) (cons (> 5 6) (* 7 8))) (cons (cons 3 #t) (cons {(> 5 6)} (* 7 8))) (cons (cons 3 #t) (cons #f {(* 7 8)})) (cons (cons 3 #t) (cons #f 56)) (cons E1 E2) * (cons V1 {E2}); first evaluate E1 to V1 step-by-step * (cons V1 V2); then evaluate e2 to v2 step-by-step 5 6 car and cdr car extracts the le\ value of a pair Prac3ce with car and cdr Write expressions using car, cdr, and tr that extract the five leaves of this tree: (car (cons 7 4)) 7 (define tr (cons (cons 17 (cons "cat" #\a)) (cons #t (λ (x) (* 2 x)))) cdr extract the right value of a pair tr (cons (cons 17 (cons "cat" #\a)) (cons #t (λ (x) (* 2 x)))), (cdr (cons 7 4)) 4 tr Why these names? car from contents of address register cdr from contents of decrement register 17 #\a #t (λ (x) (* 2 x)) 7 "cat" 8

cadr and friends (caar e) means (car (car e)) (cadr e) means (car (cdr e)) (cdar e) means (cdr (car e)) (cddr e) means (cdr (cdr e)) (caaar e) means (car (car (car e))) (cddddr e) means (cdr (cdr (cdr (cdr e)))) Evalua3on Rules for car and cdr Big-step seman,cs: E (cons V1 V2) E (cons V1 V2) [car] (car E) V1 (cdr e) v2 Small-step seman,cs: (car (cons V1 V2)) V1 [car] (cdr (cons V1 V2)) V2 [cdr] [cdr] 9 10 Seman3cs Puzzle According to the rules on the previous page, what is the result of evalua3ng this expression? (car (cons (+ 2 3) (* 5 #t))) Note: there are two ``natural answers. Racket gives one, but there are languages that give the other one! Printed Representa3ons in Racket Interpreter > (lambda (x) (* x 2)) #<procedure> > (cons (+ 1 2) (* 3 4)) '(3. 12) > (cons (cons 5 6) (cons 7 8)) '((5. 6) 7. 8) > (cons 1 (cons 2 (cons 3 4))) '(1 2 3. 4) What s going on here? 11 12

Display Nota3on, Print Nota3on and Doged Pairs The display nota,on for (cons V1 V2) is (DN1. DN2), where DN1 and DN2 are the display nota3ons for V1 and V2 In display nota3on, a dot eats a paren pair that follows it directly: ((5. 6). (7. 8)) becomes ((5. 6) 7. 8) (1. (2. (3. 4))) becomes (1. (2 3. 4)) becomes (1 2 3. 4) Why? Because we ll see this makes lists print predly. The print nota,on for pairs adds a single quote mark before the display nota3on. (We ll say more about quota3on later.) display vs. print in Racket > (display (cons 1 (cons 2 null))) (1 2) > (display (cons (cons 5 6) (cons 7 8))) ((5. 6) 7. 8) > (display (cons 1 (cons 2 (cons 3 4)))) (1 2 3. 4) > (print (cons 1 (cons 2 null))) '(1 2) > (print (cons (cons 5 6) (cons 7 8))) '((5. 6) 7. 8) > (print (cons 1 (cons 2 (cons 3 4)))) '(1 2 3. 4) 13 14 Racket interpreter uses print (quoted) nota3on > (cons 1 (cons 2 null)) '(1 2) > (cons (cons 5 6) (cons 7 8)) '((5. 6) 7. 8) > (cons 1 (cons 2 (cons 3 4))) '(1 2 3. 4) Why? Because, as we ll see later, quoted values evaluate to themselves, and so are an easy way to specify a compound data value. Without the quote, the parentheses would indicate func3on calls and would generate errors. > '(1 2) '(1 2) > '((5. 6) 7. 8) '((5. 6) 7. 8) > '(1 2 3. 4) '(1 2 3. 4) > (1 2) application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 15 Func3ons Can Take and Return Pairs (define (swap-pair pair) (cons (cdr pair) (car pair))) (define (sort-pair pair) (if (< (car pair) (cdr pair)) pair (swap pair))) What are the values of these expressions? (swap-pair (cons 1 2)) (sort-pair (cons 4 7)) (sort-pair (cons 8 5)) 16

Lists Box-and-pointer nota3on for lists In Racket, a list is just a recursive pattern of pairs. A list is either The empty list null, whose display notation is () A nonempty list (cons Vfirst Vrest) whose - first element is Vfirst - and the rest of whose elements are the sublist Vrest E.g., a list of the 3 numbers 7, 2, 4 is wrigen (cons 7 (cons 2 (cons 4 null))) A list of n values is drawn like this: V1 V2 Vn For example: 7 2 4 7 2 4 Vn Nota3on for null in box-and-pointer diagrams A pair slot containing null can also be with a slash through the slot 17 18 list sugar Treat list as syntac3c sugar:* (list)desugars to null (list E1 )desugars to (cons E1 (list )) For example: (list (+ 1 2) (* 3 4) (< 5 6)) desugars to (cons (+ 1 2) (list (* 3 4) (< 5 6))) desugars to (cons (+ 1 2) (cons (* 3 4) (list (< 5 6)))) desugars to (cons (+ 1 2) (cons (* 3 4) (cons (< 5 6) (list)))) desugars to (cons (+ 1 2) (cons (* 3 4) (cons (< 5 6) null))) Display Nota3on for Lists The dot eats parens rule makes lists display nicely: (list 7 2 4) desugars to (cons 7 (cons 2 (cons 4 null)))) displays as (before rule) (7. (2. (4. ()))) displays as (a\er rule) (7 2 4) prints as '(7 2 4) In Racket: > (cons 7 (cons 2 (cons 4 null))) '(7 2 4) * This is a white lie, but we can pretend it s true for now 19 > (list 7 2 4) '(7 2 4) 20

list and small-step evalua3on In small-step deriva3ons, it s helpful to both desugar and resugar with list: (list (+ 1 2) (* 3 4) (< 5 6)) desugars to (cons {(+ 1 2)} (cons (* 3 4) (cons (< 5 6) null))) (cons 3 (cons {(* 3 4)} (cons (< 5 6) null))) (cons 3 (cons 12 (cons {(< 5 6)} null))) (cons 3 (cons 12 (cons #t null))) resugars to (list 3 12 #t) Heck, let s just informally write this as: first, rest, and friends first returns the first element of a list: (first (list 7 2 4)) 7 (first is almost a synonym for car, but requires its argument to be a list) rest returns the sublist of a list containing every element but the first: (rest (list 7 2 4)) (list 2 4) (rest is almost a synonym for cdr, but requires its argument to be a list) (list {(+ 1 2)} (* 3 4) (< 5 6)) (list 3 {(* 3 4)} (< 5 6)) (list 3 12 {(< 5 6)}) (list 3 12 #t) 21 Also have second, third,, ninth, tenth Stylis3cally, first, rest, second, third preferred over car, cdr, cadr, caddr because emphasizes that argument is expected to be a list. 22 first, rest, and friends examples > (define L '(10 20 (30 40 50 60))) length length returns the number of top-level elements in a list: > (first L) 10 > (second L) 20 > (third L) '(30 40 50 60) > (fourth (third L)) 60 > (fourth L) fourth: list contains too few elements list: '(10 20 (30 40 50 60)) > (first '(1 2 3. 4)) first: contract violation expected: (and/c list? (not/c empty?)) given: '(1 2 3. 4) > (length (list 7 2 4)) 3 > (length '((17 19) (23) () (111 230 235 251 301))) 4 > (length '()) 0 > (length '(())) 1 > (rest (third L)) '(40 50 60) > (length '(1 2 3. 4)).. length: contract violation expected: list? given: '(1 2 3. 4) 23 24

List exercise (define LOL (list (list 17 19) (list 23 42 57) (list 110 (list 111 230 235 251 301) 304 342))) append append takes any number of lists and returns a list that combines all of the top-level elements of its argument lists. > (append '(17 19) '(23 42 57)) '(17 19 23 42 57) What is the printed representa3on of LOL? Give expressions involving LOL that return the following values: o 19 o 23 o 57 o 251 o '(235 251 301) What is the value of (+ (length LOL) (length (third LOL)) (length (second (third LOL))))? 25 > (append '(17 19) '(23 42 57) '(111) '() '(230 235 251 301)) '(17 19 23 42 57 111 230 235 251 301) > (append '((0 1) 2 (3 (4 5))) '(() (6 (7 8) 9))) '((0 1) 2 (3 (4 5)) () (6 (7 8) 9)) > (append '(0 1) 2 '(3 (4 5))).. append: contract violation expected: list? given: 2 26 cons vs. list vs. append cons, list, and append are the three most common ways to build lists. They are very different! Since you will use them extensively in both Racket and Standard ML, it s important to master them now! In the context of lists, (cons Eval Elist) creates one new cons-cell and returns a list whose length is 1 more then the length of its Vval 2 nd argument (assumed to be a list here). value of Eval (list Eval1 Eval2) creates a list of length 2 using two new cons-cells. (list Eval1 Evaln) creates a list of length n (append Elist1 Elist2) only makes sense if Elist1 and Elist2 denote lists. It returns a list whose length is the sum of the length of the two lists. append can be applied to any number of lists. List of length n+1 that s the result of cons Vval1 value of Eval1 list Vlist of length n that s the value of Elist List of length 2 that s the result of list Vval2 value of Eval2 List of length k+n that s the result of append k values from Elist1 n values from Elist2 27 append and sharing Given two lists L1 and L2, (append L1 L2) copies the list structure of L1 but shares the list structure of L2. For example: L1 8 3 (append L1 L2) 8 3 L2 7 2 4 This fact important when reasoning about number of cons-cells created by a program. We ll see why it s true in the next lecture, when we see how append is implemented Given more than two lists, append copies all but the last and only shares the last. 28

cons vs. list vs. append exercise Suppose you are given: Use (cons Eval Elist) rather than (append (list Eval) Elist) (define L1 '(7 2 4)) (define L2 '(8 3 5)) For each of the following three expressions: 1. Draw the box-and-pointer structure for its value Although (cons Eval Elist) and (append (list Eval) Elist) return equivalent lists, the former is preferred stylis3cally over the lager (because the former creates only one cons-cell, but the lager creates two). For example, use this: 2. Write the quoted notation for its value 3. Determine the length of its value > (cons (* 6 7) '(17 23 57)) '(42 17 23 57) (cons L1 L2) (list L1 L2) (append L1 L2) Rather than this: > (append (list (* 6 7)) '(17 23 57)) '(42 17 23 57) 29 30