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