CS 201: Structs

In [1]:
(require racket)
(require racket/base)
In [2]:
(struct person (name age college) #:transparent)

Constructor: person

In [3]:
(define p1 (person 'john 23 'yale))
(define p2 (person 'mary 20 'yale))
In [4]:
p1
Out[4]:
(person 'john 23 'yale)
In [5]:
p2
Out[5]:
(person 'mary 20 'yale)

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

In [6]:
(person-name p1)
Out[6]:
'john
In [7]:
(person-age p1)
Out[7]:
23
In [8]:
(person-college p2)
Out[8]:
'yale

Comparison: equal? not =

In [9]:
(equal? p1 p2)
Out[9]:
#f
In [10]:
(equal? p1 p1)
Out[10]:
#t
In [11]:
(equal? p1 (person 'john 23 'yale))
Out[11]:
#t
In [14]:
(= p1 (person 'john 23 'yale))
=: contract violation
  expected: number?
  given: (person 'john 23 'yale)
  argument position: 1st
  other arguments...:
   (person 'john 23 'yale)

predicate: person?

In [12]:
(person? p1)
Out[12]:
#t
In [13]:
(person? 'p1)
Out[13]:
#f

structs for hw3 Turing Machine

An instruction is a 5-tuple

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

In [15]:
(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)

In [17]:
(struct conf (state ltape symbol rtape) #:transparent)

A tape is just a list of symbols

In [20]:
(define tape1 '(1 1 0 1 0))
(define tape2 '(b 1 1 b 0 1 1 b b))
(define config1 (conf 'q1 '(0 1) 0 '(1 1)))
In [22]:
(conf-state config1)
Out[22]:
'q1
In [23]:
(conf-symbol config1)
Out[23]:
0
In [24]:
(conf-ltape config1)
Out[24]:
'(0 1)
In [25]:
(conf-rtape config1)
Out[25]:
'(1 1)

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

In [26]:
(define tm1 
  (list
   (ins 'q1 0 'q1 1 'R)
   (ins 'q1 1 'q1 0 'R)
   (ins 'q1 'b 'q2 'b 'L)
   (ins 'q2 0 'q2 0 'L)
   (ins 'q2 1 'q2 1 'L)
   (ins 'q2 'b 'q3 'b 'R)))
In [ ]: