datastructures in racket
play

Datastructures in Racket Part 1 For todays class, were going to - PowerPoint PPT Presentation

Datastructures in Racket Part 1 For todays class, were going to build every data structure out of three things The first is atoms These are the primitive things in the language symbol 1 These are like int and char in


  1. Datastructures in Racket Part 1

  2. For today’s class, we’re going to build every data structure out of three things

  3. The first is atoms These are the primitive things in the language

  4. ‘symbol 1

  5. These are like “ int ” and “ char ” in C++

  6. The second is the empty list

  7. ‘()

  8. The last is cons

  9. Cons is a function that takes two values and makes a pair

  10. That pair is represented as a cons cell

  11. (cons 1 2) 1 2

  12. cons is the the natural cons tructor of the language

  13. I use two strange words to refer to the elements of this cons cell

  14. “car”

  15. “car” “cdr”

  16. Because car and cdr break apart what I build with cons, I call them my destructors

  17. And that’s all

  18. And that’s all ‘sym 23 #\c Atoms ‘() Empty list (cons ‘sym 23) cons (car (cons ‘sym 23)) car/cdr

  19. Using just this, I can make a list

  20. Using just this, I can make a list (And everything else in the world, but we’ll get back to that…)

  21. If I want to make the list containing 2 I do this

  22. (cons 2 ‘()) 2 ‘()

  23. When I do this, Racket prints it out as a list

  24. ‘(2)

  25. The way to read this is “The list containing 2, followed by the empty list.”

  26. Just as I can build lists of a single element, I can build larger lists from smaller lists…

  27. And I do that by stuffing lists inside other lists…

  28. (cons 2 ‘()) 2 ‘()

  29. (cons 3 (cons 2 ‘()) ) 3 2 ‘()

  30. Racket will print this out as

  31. ‘(3 2)

  32. Of course, I probably need at least numbers as primitives right?

  33. To get the head of a list, I use car

  34. (cons 3 (cons 2 ‘()) ) 3 2 ‘()

  35. (car (cons 3 (cons 2 ‘()) ) ) 3 2 ‘()

  36. (cdr (cons 3 (cons 2 ‘()) ) ) 3 2 ‘()

  37. So now how would I get the second element?

  38. (cdr (cons 3 (cons 2 ‘()) ) ) 3 2 ‘()

  39. (car (cdr (cons 3 (cons 2 ‘()) ) ) ) 3 2 ‘()

  40. Racket abbreviates (cons 1 (cons 2 (cons…(cons n ‘())…))) as… ‘(1 2 … n)

  41. If I wanted to write out lists, I could do so using (cons 1 (cons 2 …))

  42. How do I get the nth element of a list?

  43. (define (nth list n) (if (= 0 n) (car list) (nth (cdr list) (- n 1))))

  44. Now, write (map f l)

  45. Writing lists would get quite laborious

  46. Instead, I can use the primitive function list

  47. (list 1 2 ‘serpico) ‘(1 2 serpico)

  48. Oh, and actually I can use this to represent trees too

  49. 1 2 3 4

  50. How would I build this?

  51. (define empty-tree 'empty-tree) (define (make-leaf num) num) (define (make-tree left right) (cons left right))

  52. You define (left-subtree tree)

  53. (define (least-element tree) (if (number? tree) tree (least-element (left-subtree tree))))

  54. But surely I need things like numbers right?

  55. It turns out, you could build those using just cons, car, cdr, if, =, and ‘()

  56. Define the number n as … ‘() ‘(()) ‘(() ()) …

  57. (define (weird-plus i j) (if (equal? i '()) j (weird-plus (cdr i) (cons '() j))))

  58. (weird-plus '(() ()) '(() ())) '(() () () ())

  59. It turns out, if I’m clever, we can even get rid of if and equal (Though we shall not do so here..)

  60. I can build my own datatypes in this manner

  61. I usually write constructor functions to help me build datatypes

  62. I usually write constructor functions to help me build datatypes And I usually write destructor functions to access it

  63. (define (make-complex real imag) (cons real imag)) And I usually write destructor functions to access it

  64. (define (make-complex real imag) (cons real imag)) (define (get-real complex) (car complex)) (define (get-imag complex) (cdr complex))

  65. Now, define (add-complex c1 c2)

  66. Next, define (make-cartesian x y) And the associated helper functions

  67. Next class we will talk about… struct match I/O And switch over to layout in assembly

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