topic 6
play

Topic 6 Draw cdr pointers to the right Hierarchical Data and the - PDF document

Box and pointer notation Topic 6 Draw cdr pointers to the right Hierarchical Data and the Closure Draw car pointers downward (cons 1 2) Property 2 Section 2.2.1 September 2008 1 Spring 2008 Programming Development 1 Spring 2008


  1. Box and pointer notation Topic 6 • Draw cdr pointers to the right Hierarchical Data and the Closure • Draw car pointers downward (cons 1 2) Property 2 Section 2.2.1 September 2008 1 Spring 2008 Programming Development 1 Spring 2008 Programming Development 2 Techniques Techniques Another list structure The closure property (cons (cons 1 2) (cons 3 4)) • A constructor has the closure property if it can take data of a certain type as input and return data of the same type • cons is an example 4 • Such constructors can be used to build 2 3 hiearchical structures 1 Spring 2008 Programming Development 3 Spring 2008 Programming Development 4 Techniques Techniques Lists, a recursive data type What do lists look like? (cons 1 ( cons 2 (cons 3 (cons 4 empty)))) • The empty list is a list (1 2 3 4) • If x is any datum and y is a list, then (cons x y) is a list • The empty list is denoted by empty in DrScheme and by nil in the course textbook • Whenever you see nil in the book, read empty 1 2 3 4 Spring 2008 Programming Development 5 Spring 2008 Programming Development 6 Techniques Techniques 1

  2. Lists can contain lists Box and pointer representation (cons 1 (cons (cons 2 (cons 3 empty)) (cons 4 (cons (cons 5 (cons 6 empty)) 1 4 empty)))) (1 (2 3) 4 (5 6)) 5 6 2 3 3 Spring 2008 Programming Development 7 Spring 2008 Programming Development 8 Techniques Techniques Printing out list structures A comparison Printed like lists, but if the last cdr in a cdr chain points to a primitive datum other than empty , the primitive datum is printed with a dot in front of it. 1 2 3 4 (1 2 3 4) 4 1 2 3 (1 2 3 . 4) Spring 2008 Programming Development 9 Spring 2008 Programming Development 10 Techniques Techniques Some service procedures for lists More service procedures (list 1 2 3 4) --> (1 2 3 4) (define (null? x) (equal? x empty)) ;; takes a list with at least n elements ;; and returns the nth element of the list ; takes a list and returns the number ;; note counting starts from 0 ; of elements in the list (define (our-list-ref lst n) (define (our-length list) (if (= n 0) (if (null? list) (car lst) 0 (our-list-ref (cdr lst) (+ 1 (our-length (cdr list))))) (- n 1)))) (our-list-ref (list 1 2 3 4) 0) --> 1 (our-list-ref (list 1 2 3 4) 2) --> 3 Spring 2008 Programming Development 11 Spring 2008 Programming Development 12 Techniques Techniques 2

  3. our-member ; takes an element and a list and returns non-#f if • (first-n lst n) ; ele is in the list (define (our-member ele lst) (cond ((null? lst) #f) ((equal? ele (car lst)) lst) (else (our-member ele (cdr lst))))) Spring 2008 Programming Development 13 Spring 2008 Programming Development 14 Techniques Techniques Append Notes ; takes two lists and returns a list ; containing the elements of the • Procedures list-ref , null?, eq?, length, and append are predefined procedures in ; original 2 Scheme (define (our-append list1 list2) (if (null? list1) list2 • Procedure append can append any number of (cons (car list1) lists together (our-append (cdr list1) list2)))) • Procedure pair? returns #t if its argument is a pair, else #f (our-append (list 1 2) (list 3 4)) --> (1 2 3 4) Spring 2008 Programming Development 15 Spring 2008 Programming Development 16 Techniques Techniques 3

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