Match
Use match
as much as possible.
Match documentation in the Racket Guide and in the Racket Reference.
The exercises on this page are intended to use:
- Basic matching of symbols and other constants.
...
- Unlimited repetition...N
- Limited repetition.
- The function
q1
takes in a symbol and puts out a number. If the symbol is not in the table, output 0. Just use amatch
.
Symbol | ‘one | ’two | ’three | ’thirty |
---|---|---|---|---|
Number | 1 | 2 | 3 | 30 |
-
(
q2
) In this exercise, you will write a function that makes pairs with program labels and the line numbers on which they occur. The setup here is that you have a list containing a line number and a Hack instruction in parentheses. For example, the Hack program@20 D=0 (REPEAT)
would be represented in line-numbered list form as:
'((1 (A 20)) (2 (C D=0)) (3 (L REPEAT)))
The output from
q2
running on that program should be:'(() () (REPEAT 3))
That is, a list with the label and its line number. The empty list was chosen as the output in the other cases because it is easy to work with.
Another example of this function working:
(check-expect (q2 (list '(10 (L WOW))))
(list '(WOW 10)))
(check-expect (q2 (list '(20 (A NOPE))))
(list '()))
Data to test your program:
(define ex2-data
'((1 (A num))
(2 (L AFTER-LOOP))
(3 (A 50))
(4 (L WORK))
(5 (C onfusion))))
(define ex2-correct
'(()
(AFTER-LOOP 2)
()
(WORK 4)
()))
(check-expect (q2 ex2-data)
ex2-correct)
-
(
q3
) You are given a list of lists. Some of those lists begin with the symbol'Info
. Extract the list of symbols given after'Info
, ignoring the rest of the lists.Use a recursive function that does pattern matching in the body.
(define ex3-data
'((Info The first line is good)
(A 50)
(Info After A is still working ok)
(L WORK)
(A 0)
(Info Entering loop)))
(define ex3-correct
'((The first line is good)
(After A is still working ok)
(Entering loop)))
(check-expect (q3 ex3-data)
ex3-correct)
- (
q4
) Retain the side lengths from the list when they are already tagged'acute
. Do not actually do math to check to see if the triangle is acute.
(define q4-data
'((5 12 13 right)
(8 15 20 obtuse)
(3 4 4 acute)))
(define q4-correct
'(() () (3 4 4)))
(check-expect (q4 q4-data)
q4-correct)
- Learn how
...
(repeat 0+ times) and..k
(repeat k+ times) pattern matching works. Note that you always get a list from these, so name your variables appropriately. (Nothing to do.)
(match (list 1 2 3 4 5 6 7 8 9 10)
[(list xs ... endie)
(list xs endie)])
(match (list 1 1 1 1 8 9 10)
[(list 1 ... more ... endie)
(list more endie)])
(match (list 1 2 3 4 5 6 7 8 9 10)
[(list xs ..5 ys ..2 z)
(list xs ys z)])
-
(
q6
) (FIXME - edit so that it is easier to write the match?) The intent in this question is for you to practice the...
or..k
match form.Given a list like one of these:
(define q6-exA (list 1 2 3 4 5 6 7 8 'add)) (define q6-exB (list 11 12 13 14 15 16 17 18 19 20 'multiply))
- When the list ends in
'add
, the answer is sum of the fourth from last and the next-to-last item in the list. - When the list ends in
'multiply
, find the product of all of the numbers beginning with the 8th1. You may assume that the only non-number is the last item in the list. - Otherwise output 0.
Assume that there are at least 9 numbers in the list.
Checks:
(check-expect (q6 q6-exA) (+ 5 8)) (check-expect (q6 q6-exB) (* 18 19 20)) (check-expect (q6 (list 21 'nope)) 0)
- When the list ends in