CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
When presenting each data structure we will introduce two sets of functions:
Constructors: functions needed to build the data structure
Accessors: functions needed to retrieve each component of the data structure. Also known as selectors.
Each example we discuss is prefaced by some unit tests. We are following a Test Driven Development methodology.
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
empty: creates an empty listlist: creates a list by specifying its elementscons: adds an element to the left-hand-side (the end) of the listCS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
empty'()CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
listexpression = $\cdots$ | list
list = (list expression* )
Function call list constructs a list with the evaluation of a possibly-empty sequence of expressions e1 up to en as values v1 up to vn which Racket prints as: '(v1 ... vn)
#lang racket
(list (+ 0 1) (+ 0 1 2) (+ 0 1 2 3))
(list)
$ racket list-ex1.rkt
'(1 3 6)
'()
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
cons(define l (list 1 2 3))
(cons 5 l)
; '(5 1 2 3)
There is no direct constructor to add an element to the right, but we can implement one.
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
#lang racket
(require rackunit)
(check-equal?
(cons 1
(cons 2
(cons 3
(cons 4 empty)))) (list 1 2 3 4))
'(1 .
'(2 .
'(3 .
'(4 . '()))))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
empty?You can test if a list is empty with function empty?. An empty list is printed
as '().
#lang racket
(require rackunit)
(check-false (empty? (list (+ 0 1) (+ 0 1 2) (+ 0 1 2 3))))
(check-true (empty? (list)))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
first, restLists in Racket are implemented as a linked-list using pairs terminated by the empty list '().
Function first returns the head of the list, given a nonempty list.
Function rest returns the tail of the list, given a nonempty list.
(list 1 2 3 4)
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
We can represent data-structures using pairs/lists.
For instance, let us build a 3-D point data type.
(require rackunit)
(define p (point 1 2 3))
(check-true (point? p))
(check-equal? (list 1 2 3) p)
(check-equal? 1 (point-x p))
(check-equal? 2 (point-y p))
(check-equal? 3 (point-z p))
(check-true (origin? (list 0 0 0)))
(check-false (origin? p))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
We can represent data-structures using pairs/lists.
For instance, let us build a 3-D point data type.
(require rackunit)
(define p (point 1 2 3))
(check-true (point? p))
(check-equal? (list 1 2 3) p)
(check-equal? 1 (point-x p))
(check-equal? 2 (point-y p))
(check-equal? 3 (point-z p))
(check-true (origin? (list 0 0 0)))
(check-false (origin? p))
; Constructor
(define (point x y z) (list x y z))
(define (point? x)
(and (list? x)
(= (length x) 3)))
; Accessors
(define (point-x pt) (first pt))
(define (point-y pt) (second pt))
(define (point-z pt) (third pt))
; Example function
(define (origin? p) (equal? p (list 0 0 0)))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
(define (point x y z) (list x y z))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
(define (point x y z) (list x y z))
def set_x(self, x):
return Point(x, p.y, p.z)
(define (point-set-x p x)
(point x (point-y p) (point-z p)))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
(define (point x y z) (list x y z))
def set_x(self, x):
return Point(x, p.y, p.z)
(define (point-set-x p x)
(point x (point-y p) (point-z p)))
def set_y(self, y):
return Point(p.x, y, p.z)
(define (point-set-y p y)
(point (point-x p) y (point-z p)))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
(define (point x y z) (list x y z))
def set_x(self, x):
return Point(x, p.y, p.z)
(define (point-set-x p x)
(point x (point-y p) (point-z p)))
def set_y(self, y):
return Point(p.x, y, p.z)
(define (point-set-y p y)
(point (point-x p) y (point-z p)))
def set_z(self, z):
return Point(p.x, p.y, z)
(define (point-set-z p z)
(point (point-z p) (point-y p) z))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
obj.field:# pt.x
# pt.y
# pt.z
(define (point-x pt) (first pt))
(define (point-y pt) (second pt))
(define (point-z pt) (third pt))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
obj.field:# pt.x
# pt.y
# pt.z
(define (point-x pt) (first pt))
(define (point-y pt) (second pt))
(define (point-z pt) (third pt))
pt1.set_x(pt2.y + pt3.z)
(point-set-x pt1
(+
(point-y pt2)
(point-z pt3)
)
)
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
Function (quote e) serializes expression e. Note that expression e is not evaluated.
x becomes a symbol 'x. You can consider a symbol to be a special kind of string in Racket. You can test if an expression is a symbol with
function symbol?(define x e) yields a list with: symbol 'define, the serialization of variable x, and
the serialization of e. Serializing $(\mathtt{define}\ (x_1 \cdots x_n)\ e)$ yields a list with symbol 'define followed by a nonempty list of symbols $\mathtt{'}x_i$ followed by serialized $e$.'lambda, followed by a possibly-empty list of symbols $x_i$, and the serialized expression $e$.'cond followed by a serialized branch. Each branch is a list
with two components: serialized expression $b_i$ and serialized expression
$e_i$.CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
'term rather than (quote term)(lambda (x) x) with quote?(+ 1 2) with quote?(cond [(> 10 x) x] [else #f]) with quote?CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
'term rather than (quote term)(lambda (x) x) with quote?(+ 1 2) with quote?(cond [(> 10 x) x] [else #f]) with quote?(. Quote only accepts S-expressions (parentheses must be well-balanced, identifiers must be valid Racket identifiers, number literals must be valid).CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
'term rather than (quote term)(lambda (x) x) with quote?(+ 1 2) with quote?(cond [(> 10 x) x] [else #f]) with quote?(. Quote only accepts S-expressions (parentheses must be well-balanced, identifiers must be valid Racket identifiers, number literals must be valid).(lambda)CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
#lang racket
(require rackunit)
(check-equal? 3 (quote 3)) ; Serializing a number returns the number itself
(check-equal? 'x (quote x)) ; Serializing a variable named x yields symbol 'x
(check-equal? (list '+ 1 2) (quote (+ 1 2))) ; Serialization of function as a list
(check-equal? (list 'lambda (list 'x) 'x) (quote (lambda (x) x)))
(check-equal? (list 'define (list 'x)) (quote (define (x))))
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
list, must check:
list?length'definesymbol?+ means >= 1* means >= 0CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
program = #lang racket term*
term = definition | expression
definition = basic-def | function-def
basic-def = ( define identifier expression )
function-def = ( define (identifier+ ) term+ )
expression = value | identifier | function-call | function-dec | $\cdots$
value = number | $\cdots$
function-call = ( expression+ )
function-dec = ( lambda ( identifier* ) term+)
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
basic-def = ( define identifier expression )
node is a listnode has 3 elementsdefine is the first element of the list (what function can you use?)node) is a symbolfunction-call = ( expression+ )
node is a listnode has at least 1 element (because of operator +)CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
lambda?, define?, and define-func?.lambda?define-func?define? by calling define-func? and define-basic? (do not copy/paste code)define-func? should pass in define?CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
We are grading the correctness of a solution
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
Software engineering and academic life is about communication: you are expected to interact to solve your homework assignments.
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro
CS450 ☽ Lists and code serialization ☽ Lecture 3 ☽ Tiago Cogumbreiro