Racket Values booleans: #t, #f numbers: The Pros of cons : - - PowerPoint PPT Presentation

racket values
SMART_READER_LITE
LIVE PREVIEW

Racket Values booleans: #t, #f numbers: The Pros of cons : - - PowerPoint PPT Presentation

Racket Values booleans: #t, #f numbers: The Pros of cons : integers: 42 , 0, -273 Pairs and Lists in Racket ra3onals: 2/3 , -251/17 floa3ng point (including scien3fic nota3on): 98.6 , -6.125 , 3.141592653589793, 6.023e23


slide-1
SLIDE 1

The Pros of cons: Pairs and Lists in Racket

CS251 Programming Languages

Spring 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 Pairs and Lists

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)
  • (cons "CS251" (λ (x) (* 2 x))
  • (cons (cons 3 4.5) (cons #f #\a))
  • Can glue any number of values into a cons tree!

In Racket, type Command-\ to get λ char

3 Pairs and Lists

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 "cat" #t (λ (x) (* 2 x))

4 Pairs and Lists

slide-2
SLIDE 2

Evalua3on Rules for cons

Big step seman,cs: E1 ↓ V1 E2 ↓ V2 (cons E1 E2) ↓ (cons V1 V2)

[cons]

5 Pairs and Lists

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 E1 E2) ⇒* (cons V1 {E2}); first evaluate E1 to V1 step-by-step ⇒* (cons V1 V2); then evaluate e2 to v2 step-by-step

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

6 Pairs and Lists

car and cdr

  • car extracts the le\ value of a pair

(car (cons 7 4))⇒ 7

  • cdr extract the right value of a pair

(cdr (cons 7 4))⇒ 4 Why these names?

  • car from “contents of address register”
  • cdr from “contents of decrement register”

7 Pairs and Lists

Prac3ce with car and cdr

(define tr (cons (cons 17 (cons "cat" #\a)) (cons #t (λ (x) (* 2 x))))

17 #\a "cat" #t (λ (x) (* 2 x)) Write expressions using car, cdr, and tr that extract the five leaves of this tree:

8 Pairs and Lists

tr ⟼ tr ⟼ (cons (cons 17 (cons "cat" #\a)) (cons #t (λ (x) (* 2 x)))), …

slide-3
SLIDE 3

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

9 Pairs and Lists

(car (cons V1 V2)) ⇒ V1 [car]

Evalua3on Rules for car and cdr

Big-step seman,cs: E ↓ (cons V1 V2) (car E) ↓ V1 [car] E ↓ (cons V1 V2) (cdr e) ↓ v2 [cdr] Small-step seman,cs:

10 Pairs and Lists

(cdr (cons V1 V2)) ⇒ V2 [cdr]

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!

11 Pairs and Lists

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?

12 Pairs and Lists

slide-4
SLIDE 4
  • 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 Nota3on, Print Nota3on and Doged Pairs

13 Pairs and Lists

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

display vs. print in Racket

14 Pairs and Lists

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

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

Racket interpreter uses print (quoted) nota3on

15 Pairs and Lists

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...:

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 Pairs and Lists

slide-5
SLIDE 5

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

17 Pairs and Lists

Box-and-pointer nota3on for lists

V1 V2 Vn

  • A list of n values is drawn like this:

Nota3on for null in box-and-pointer diagrams

For example: 7 2 4

18 Pairs and Lists

Vn 7 2 4

A pair slot containing null can also be with a slash through the slot

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

* This is a white lie, but we can pretend it’s true for now

19 Pairs and Lists

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) > (list 7 2 4) '(7 2 4)

20 Pairs and Lists

slide-6
SLIDE 6

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:

(list {(+ 1 2)} (* 3 4) (< 5 6)) ⇒ (list 3 {(* 3 4)} (< 5 6)) ⇒ (list 3 12 {(< 5 6)}) ⇒ (list 3 12 #t)

21 Pairs and Lists

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)

  • Also have second, third, …, ninth, tenth
  • Stylis3cally, first, rest, second, third preferred
  • ver car, cdr, cadr, caddr because emphasizes that

argument is expected to be a list.

22 Pairs and Lists

first, rest, and friends examples

23 Pairs and Lists

> (define L '(10 20 (30 40 50 60)))
 > (first L) 10
 > (second L) 20
 > (third L) '(30 40 50 60)
 > (fourth (third L)) 60
 > (rest (third L)) '(40 50 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

length returns the number of top-level elements in a list:

24 Pairs and Lists

> (length (list 7 2 4)) 3 > (length '((17 19) (23) () (111 230 235 251 301))) 4 > (length '()) > (length '(())) 1 > (length '(1 2 3 . 4)) . . length: contract violation expected: list? given: '(1 2 3 . 4)

slide-7
SLIDE 7

List exercise

(define LOL (list (list 17 19) (list 23 42 57) (list 110 (list 111 230 235 251 301) 304 342)

25 Pairs and Lists

  • What is the printed representa3on of LOL?
  • Give expressions involving LOL that return the following values:
  • 19
  • 23
  • 57
  • 251
  • '(235 251 301)
  • What is the value of

(+ (length LOL) 
 (length (third LOL)) 
 (length (second (third LOL))))?

append

append takes any number of lists and returns a list that combines all of the top-level elements of its argument lists.

26 Pairs and Lists

> (append '(17 19) '(23 42 57)) '(17 19 23 42 57) > (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 (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.

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!

27 Pairs and Lists

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 2nd argument (assumed to be a list here). (list Eval1 Eval2) creates a list of length 2 using two new cons-cells. (list Eval1 … Evaln) creates a list of length n

list Vlist of length n that’s the value of Elist value of Eval Vval List of length n+1 that’s the result of cons value of Eval1 Vval1 List of length 2 that’s the result of list value of Eval2 Vval2

n values from Elist2 List of length k+n that’s the result of append

k values from Elist1

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.

28 Pairs and Lists

For example: 7 2 4 8 3 8 3 L1 L2 (append L1 L2)

  • 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.
slide-8
SLIDE 8

cons vs. list vs. append exercise

(define L1 '(7 2 4)) (define L2 '(8 3 5))

29 Pairs and Lists

Suppose you are given:

(cons L1 L2) (list L1 L2) (append L1 L2)

For each of the following three expressions:

  • 1. Draw the box-and-pointer structure for its value
  • 2. Write the quoted notation for its value
  • 3. Determine the length of its value

Use (cons Eval Elist) rather than (append (list Eval) Elist)

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:

30 Pairs and Lists

> (cons (* 6 7) '(17 23 57)) '(42 17 23 57) Rather than this: > (append (list (* 6 7)) '(17 23 57)) '(42 17 23 57)