You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: labs/lab06.md
+18-18Lines changed: 18 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,13 +14,13 @@ pointer pointing to an active number. Brainf\*ck programs specify computations o
14
14
| ------ | ---------- | ------- |
15
15
| < | < | move pointer to the left by one step |
16
16
| > | > | move pointer to the right by one step |
17
-
| + | + | increase the active number determined by pointer by one |
18
-
| - | - | decrease the active number determined by pointer by one |
17
+
| + | + | increase the active number determined by the pointer by one |
18
+
| - | - | decrease the active number determined by the pointer by one |
19
19
| , | @ | read one number from input and store it |
20
20
| . | * | display the active number|
21
21
|[`code`]|[`code`]| while the active number is not zero, execute `code`|
22
22
23
-
As I explained in the lecture, Racket can represent any tree structure as data. Thus we don't need
23
+
As I explained in the lecture, Racket can represent any tree structure as data. Thus, we don't need
24
24
to write a parser and represent Brainf*ck directly as a list of commands where nested lists recursively represent cycles. The only limitation with this approach is the special meaning of symbols `,`
25
25
and `.`
26
26
in Racket. So we replace them with symbols `@ *`
@@ -39,16 +39,16 @@ containing a mutable fixed-size vector representing the tape and a number repres
39
39
The goal of this lab is to modify my implementation from the lecture so that it is purely functional.
40
40
This means you must replace the tape object with a purely functional data structure. Next, the implementation of the interpreter
41
41
has to be modified a bit. My implementation creates a global tape that the interpreter modifies during the computation.
42
-
However, this is not a purely functional code. Thus the tape has to be a part of the state of the computation included among interpreter's
42
+
However, this is not a purely functional code. Thus, the tape has to be a part of the state of the computation included among interpreter's
43
43
accumulators.
44
44
45
45
## Task 1
46
46
Create a purely functional representation of the tape of a fixed size with a pointer. I will provide detailed hints on how to proceed.
47
47
Define a structure `tape` consisting of three components
48
48
`left, val, right`
49
-
where `left` is the list representing numbers on the tape stored left from the pointer,
49
+
where `left` is the list representing numbers on the tape stored to the left of the pointer,
50
50
`val`
51
-
represents the active number and `right` is the list representing the number on the right.
51
+
represents the active number, and `right` is the list representing the number on the right.
52
52
53
53
```racket
54
54
(struct tape (left val right) #:transparent)
@@ -107,8 +107,8 @@ consisting of numbers 1,2,3,4,5,6,7 with 4 being the active number.
107
107
Modify the implementation of the interpreter from the lecture so that it uses your purely functional tape.
108
108
109
109
::: tip Hint
110
-
The implementation from the lecture uses a global mutable tape which is defined once and then only reset when executing a program.
111
-
Apart from the tape, the interpreter keeps its state in accumulators `prg`
110
+
The implementation from the lecture uses a global mutable tape, which is defined once and then only reset when executing a program.
111
+
Apart from the tape, the interpreter keeps its state in the accumulators `prg`
112
112
containing remaining commands and `input` storing the remaining input. You have to add one more accumulator tracking the tape.
113
113
:::
114
114
@@ -122,21 +122,21 @@ For your convenience, my complete implementation is shown below.
122
122
#lang racket
123
123
124
124
;;; Brainf*ck - interpreter
125
-
;;; As the symbols ., have their specific meaning in Racket, we replace them by the symbols *@
125
+
;;; As the symbols ., have their specific meaning in Racket, we replace them with the symbols *@
126
126
127
127
;;; Sample Brainf*ck programs
128
-
; program reading non-negative numbers on the first two positions and displaying their sum
128
+
; program reading non-negative numbers in the first two positions and displaying their sum
129
129
(define add-prg
130
130
'(@ > @ [- < + >] < *))
131
131
132
-
; program reading non-negative numbers on the first two positions and displaying their product
132
+
; program reading non-negative numbers in the first two positions and displaying their product
0 commit comments