Lecture 2026-02-10

vectors

Primitive data structure: the array

  • a "slice" of memory

  • a sequence of consecutive memory

will dicuss at length when we return to C.

  • Racket ( Schmee) : the vector

    • used much like traditional array

      • Unlike arrays- can hold items of any size ( unlimited integers, strings, whatever )

Vectors in racket.

(define x (vector `blue #t "you"))  3-item vector
 
(define y (make-vector 10))
(define y (make-vector 10 5))
(define z (build-vector 10 sqr) 

  • Main advantage of vectors over lists:

    • vector-set! runs in O(1) time

  • Main disadvantages of vectors over lists:

    • size is fixed

    • diifficult to add or remove elements

    • tends to force an imperative style

      • Imagine map, adds cons and cons until recursion bottles out,

        • Thus list is built incremently. this is exactly how lists work in racket.

          • BUT since vectors cannot grow, TO USE VECTORS, one may deduce the size of the vector BEFORE HAND

            • THEN MUTATE THE VALUES IN THE VECTOR.

              • THIS IS AN INHERENTLY NON-FUNCTIONAL WAY TO GO ABOUT IT.

lets build my-build-vector

  • Observe that it creates the result first before modifying contents.

  • Vectors work well with imperative-style algorithms

    • for, for/vector in Racket helps with this.

  • What's a macro

    • A way of building your own syntax in Racket/Scheme

    A function whose input and output are syntax !

    • for being a macro, thus translates to another scheme and racket code that actually operates as advertised !

      • Hence we can mostly use it as a function

;; sums the elements in a vector

  • Doesn't this look like a loop? We have seen anything with tail recursion may likely be rewritten as a loop

  • notice how if we are only given sum-vector it would be impossible to know if its functional or not.

    • Hence:

circle-info

Not pure functional— since uses mutation. But looks purely functional.

  • use of mutation confined to the intervals of sum-vector

  • can't be detected outside the function

  • outsiders could consider it functional !

  • Providees a strategy for keeping problems with mutation udner control

    • Hide it behind a pure functional interface

Recall:

Drawing
  • How does this work

    • Memory only holds for fixed-size data !

      • But there's still unlimited numbers?

      • strings?

Recall:

vs.

  • directly translating in C we realize doesn't work!

circle-info

Racket structs aren't like C structs

  • the struct is copied in Racket, but change to the fields still persist.

  • the fields of a struct are boxed - they're pointers.

  • Similarily, the items in a Racket vector are addresses that point to the actual contents ( which can then be any size )!

Similarily, the fields of a cons are pointers:

Box + pointer diagram:

Drawing
  • Also since Racket is dynamically typed, the values 1, `blue, #t, must include type information.

    • More later.

"Vectors" in C: Arrays

  • array: sequence of conseccutive memory locations.

e.g.

circle-info

a[i] - access i-th item of array n

What happens if you go out of bounds? UB

Will it stop you? No.

Program may or may not crash.

  • If not, data may be corrupted; no way to detect.

  • can give the bounds implicity:

  • amt of space occupied by grades ( 20 bytes )

  • amt of space an int occupies ( 4 bytes )

Last updated