Skip to content

Commit 42bee69

Browse files
committed
Assignment 8.
1 parent 8a9b131 commit 42bee69

File tree

3 files changed

+120
-6
lines changed

3 files changed

+120
-6
lines changed

www/assignments.scrbl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@include-section{assignments/6.scrbl}
1212
@include-section{assignments/7.scrbl}
1313
@include-section{assignments/8.scrbl}
14-
@include-section{assignments/9.scrbl}
14+
@;include-section{assignments/9.scrbl}
1515
@;include-section{assignments/10.scrbl}
1616

1717

www/assignments/8.scrbl

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,123 @@
11
#lang scribble/manual
22
@(require "../defns.rkt")
3-
@title[#:tag "Assignment 8" #:style 'unnumbered]{Assignment 8: Functions with default arguments}
3+
@title[#:tag "Assignment 8" #:style 'unnumbered]{Assignment 8: Patterns}
4+
5+
@(require (for-label (except-in racket ...)))
6+
@(require "../notes/ev.rkt"
7+
"../notes/utils.rkt")
48

5-
@(require (for-label a86 (except-in racket ...)))
69

710
@bold{Due: @assign-deadline[8]}
811

9-
Details of this assignment will be released later in the semester.
12+
The goal of this assignment is to extend a compiler with new pattern
13+
matching forms for matching lists, vectors, and predicates.
14+
15+
You are given a file @tt{knock-plus.zip} on ELMS with a starter
16+
compiler similar to the @seclink["Knock"]{Knock} language we studied
17+
in class. You are tasked with:
18+
19+
@itemlist[
20+
21+
@item{implementing the @tt{list} pattern,}
22+
23+
@item{implementing the @tt{vector} pattern, and}
24+
25+
@item{implementing the @tt{?} pattern.}
26+
]
27+
28+
The following files have already been updated for you @bold{and should
29+
not be changed by you}:
30+
31+
@itemlist[ @item{@tt{ast.rkt}}
32+
@item{@tt{parse.rkt}}
33+
@item{@tt{interp.rkt}}
34+
@item{@tt{interp-prim.rkt}}
35+
@item{@tt{compile-op.rkt}}
36+
]
37+
38+
So you will only need to modify:
39+
@itemlist[
40+
@item{@tt{compile.rkt}}
41+
]
42+
to correctly implement the new features. These features are described below.
43+
44+
As a convenience, two new n-ary primitives have been added (and fully
45+
implemented): @racket[list] and @racket[vector]. The @racket[list]
46+
primitive takes any number of arguments and produces a list containing
47+
the arguments as elements; the @racket[vector] primitive does the
48+
same, but constructs a vector.
49+
50+
@ex[
51+
(list)
52+
(list 1 2 3)
53+
(list 1 #t #\c)
54+
(vector)
55+
(vector 1 2 3)
56+
(vector 1 #t #\c)]
57+
58+
These are not directly useful in implementing the patterns above, but
59+
do make it easier to write examples and tests.
60+
61+
@section[#:tag-prefix "a5-" #:style 'unnumbered #:tag "list"]{List patterns}
62+
63+
The @racket[(list _p1 ... _pn)] pattern matches a list of elements. The
64+
pattern matches a list with as many elements as there are patterns
65+
@racket[_p1] through @racket[_pn] and each element must match the
66+
respective pattern.
67+
68+
69+
@ex[
70+
(match (list)
71+
[(list) #t]
72+
[_ #f])
73+
(match (list 1 2 3)
74+
[(list x y z) x])
75+
(match (list (list 1) (list 2))
76+
[(list (list x) (list 2)) x])
77+
]
78+
79+
@section[#:tag-prefix "a5-" #:style 'unnumbered #:tag "vector"]{Vector patterns}
80+
81+
The @racket[(vector _p1 ... _pn)] pattern matches a vector of elements. The
82+
pattern matches a vector with as many elements as there are patterns
83+
@racket[_p1] through @racket[_pn] and each element must match the
84+
respective pattern.
85+
86+
87+
@ex[
88+
(match (vector)
89+
[(vector) #t]
90+
[_ #f])
91+
(match (vector 1 2 3)
92+
[(vector x y z) x])
93+
(match (vector (vector 1) (vector 2))
94+
[(vector (vector x) (vector 2)) x])
95+
]
96+
97+
@section[#:tag-prefix "a5-" #:style 'unnumbered #:tag "vector"]{Predicate patterns}
98+
99+
The @racket[(? _f)] pattern matches any value for which the predicate
100+
@racket[_f] returns a true value (any value other than @racket[#f])
101+
when applied to the value being matched. In Knock+, @racket[_f] must be
102+
the name of a user defined function.
103+
104+
@ex[
105+
(define (is-eight? x) (= x 8))
106+
(define (id x) x)
107+
108+
(match 8
109+
[(? is-eight?) #t]
110+
[_ #f])
111+
(match (vector 1 2 3)
112+
[(and (? id) x) x])
113+
(match 16
114+
[(? is-eight?) #t]
115+
[_ #f])
116+
]
117+
118+
119+
@section[#:tag-prefix "a5-" #:style 'unnumbered]{Submitting}
120+
121+
Submit a zip file containing your work to Gradescope. Use @tt{make
122+
submit.zip} from within the @tt{knock-plus} directory to create a zip
123+
file with the proper structure.

www/defns.rkt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
"Thursday, October 23, 11:59PM"
6262
"Tuesday, November 4, 11:59PM"
6363
"Tuesday, November 11, 11:59PM"
64-
"Thursday, November 20, 11:59PM"
65-
"Thursday, December 4, 11:59PM")
64+
"Thursday, December 4, 11:59PM"
65+
#;"Thursday, December 4, 11:59PM")
6666
(sub1 i)))
6767

6868
(define office-hours

0 commit comments

Comments
 (0)