racket values
play

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


  1. 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 – complex: 3+2i , 17-23i , 4.5-1.4142i Note: some are exact , the rest are inexact . See docs. • strings: "cat" , "CS251" , " αβγ " , CS251 Programming "To be\nor not\nto be" Languages • characters: #\a , #\A , #\5 , #\space , #\tab , #\newline Spring 2018, Lyn Turbak • anonymous func3ons: (lambda (a b) (+ a (* b c))) Department of Computer Science What about compound data? Wellesley College Pairs and Lists 2 Box-and-pointer diagrams for cons trees cons Glues Two Values into a Pair (cons v1 v2 ) v1 v2 A new kind of value: Conven3on: put “small” values (numbers, booleans, characters) inside a box, • pairs (a.k.a. cons cells): (cons V1 V2 ) and draw a pointers to “large” values (func3ons, strings, pairs) outside a box. e.g., (cons (cons 17 (cons "cat" #\a)) In Racket, - (cons 17 42) (cons #t ( λ (x) (* 2 x)))) type Command-\ to get λ char - (cons 3.14159 #t) - (cons "CS251" ( λ (x) (* 2 x)) - (cons (cons 3 4.5) (cons #f #\a)) 17 #t • Can glue any number of values into a cons tree! #\a (λ (x) (* 2 x)) "cat" Pairs and Lists 3 Pairs and Lists 4

  2. Evalua3on Rules for cons cons evalua3on example Big step seman,cs: (cons (cons {(+ 1 2)} (< 3 4)) E1 ↓ V1 (cons (> 5 6) (* 7 8))) ⇒ (cons (cons 3 {(< 3 4)}) E2 ↓ V2 [cons] (cons (> 5 6) (* 7 8))) (cons E1 E2 ) ↓ (cons V1 V2 ) ⇒ (cons (cons 3 #t) (cons {(> 5 6)} (* 7 8))) ⇒ (cons (cons 3 #t) (cons #f {(* 7 8)})) Small-step seman,cs: ⇒ (cons (cons 3 #t) (cons #f 56)) 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 Pairs and Lists 5 Pairs and Lists 6 Prac3ce with car and cdr car and cdr Write expressions using car , cdr , and tr that extract the five leaves of this tree: • car extracts the le\ value of a pair (define tr (cons (cons 17 (cons "cat" #\a)) (car (cons 7 4)) ⇒ 7 (cons #t ( λ (x) (* 2 x)))) tr ⟼ (cons (cons 17 (cons "cat" #\a)) • cdr extract the right value of a pair (cons #t ( λ (x) (* 2 x)))), … (cdr (cons 7 4)) ⇒ 4 tr ⟼ Why these names? 17 #t • car from “contents of address register” #\a (λ (x) (* 2 x)) • cdr from “contents of decrement register” "cat" Pairs and Lists 7 Pairs and Lists 8

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

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

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

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend