1 / 15

Structuring data

Structuring data. So far we’ve seen two types of values that we can bind to names: numbers Functions Today we will see two more: symbols pairs. Symbols. A symbol is an unevaluated name. Consider the following interaction: > (define x 12) > x 12

idola
Download Presentation

Structuring data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structuring data • So far we’ve seen two types of values that we can bind to names: • numbers • Functions • Today we will see two more: • symbols • pairs

  2. Symbols • A symbol is an unevaluated name. • Consider the following interaction: > (define x 12) > x 12 • The x in the define is not evaluated. It is a symbol. • The x in the second line is evaluated by looking up the value it is bound to in the current environment.

  3. Using symbols • We can bind a name to a symbolic value by quoting the symbol – quoting prevents evaluation: > (define fifi 'poodle) > fifi poodle • The quote mark (') is syntactic sugar for the form (quote …); the above is equivalent to: > (define fifi (quote poodle))

  4. Pairs • cons is a primitive function which builds a pair. • A pair has two members. • (cons 'a 'b) builds a pair • whose first member is the symbol a • whose second member is the symbol b • cons is therefore a pair constructor. • Graphically: a b

  5. accessors • car returns the first member of a pair • cdr returns the second member of a pair > (define myPair (cons 'a 'b)) > (car myPair) a > (cdr myPair) b

  6. How is a pair displayed? > (define myPair (cons 'a 'b)) > myPair (a . b) • Pairs are printed with a dot (.) separating the first (left) and second (right) components.

  7. The REPL • When interacting with a Scheme system, a Read-Eval-Print Loop (REPL) reads what you type, evaluates it, prints the resulting value, and loops back to repeat. • The reader and printer parts of the REPL handle syntactic sugar (like converting 'a into (quote a)) and also printing things like lists.

  8. Lists • What is a list? • A recursive definition: • the empty list () is a list • a pair whose cdr is a list is a list • We can build a list using cons and (): > (define myList (cons 'a '())) > myList (a)

  9. Wait a minute!! > (define myPair (cons 'a 'b)) > myPair (a . b) > (define myList (cons 'a '()) > myList (a)

  10. What's going on? • Why did myList print as (a) rather than as (a . ())? • In fact, they are entirely equivalent: the REPL strikes again. The printer formats pairs which cdr is a pair in a special way to make lists easier to read.

  11. Printing lists • (a . (b . (c . (d . ())))) is printed as (a b c d) • Graphically the structure is: • This structure can be built in a variety of ways, some of which are shown on the next slide. a b c d

  12. Choices…choices • (cons 'a (cons 'b (cons 'c (cons 'd '())))) • (list 'a 'b 'c 'd) • '(a b c d) • '(a . (b c d)) • (cons 'a (list 'b 'c 'd)) and so on!

  13. Association lists • An association list is a list of pairs. • Used to implement a look-up table of key-value pairs. • The primitive assoc is used to search an association list, given a key. • Example on next slide.

  14. > (define aList '((fifi . poodle) (fido . bulldog) (clifford . bigRed) (lassie . collie))) > (assoc 'fido aList) (fido . bulldog) > (assoc 'rufus aList) #f

  15. Mutating pair components • set-car! mutates the value of the car of a pair • set-cdr! mutates the value of the cdr of a pair > (define myPair (cons 'a 'b)) > myPair (a . b) > (set-car! myPair 'fred) > myPair (fred . b) > (set-cdr! myPair 'wilma) > myPair (fred . wilma)

More Related