CS 201: Structs

Racket documentation for structs.

More struct documentation.

We want to define a data structure for a person, comprising the person's name, age, and college. In racket, we could use a list, such as the following.

We can access the slots using the standard selectors, such as car, cdr, first, etc.

The problem is that someone looking at your code -- including you -- does not know that (car person1) is the name. Also, we won't accidentally

We solve this problem and make our code

The solution in racket is to use struct, which facilitates named access to data structures. Here is struct in action.

Constructor: person

The name of the struct is also the constructor.

Selectors: person-name, person-age, person-college

The selectors for the struct are the name with a hyphen prefixed to the slot names.

Comparison: equal? not =

struct comparison uses equal? not =.

predicate: person?

The predicate for a struct is the name with a ? suffix.

structs for hw3 Turing Machine

An instruction is a 5-tuple

(struct ins (c-state c-symbol n-state n-symbol dir) #:transparent)

A configuration is a current state, a head position, and a tape

(struct conf (state ltape symbol rtape) #:transparent)

A tape is just a list of symbols

A Turing Machine that toggle's 0's and 1's