-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.carp
More file actions
216 lines (201 loc) · 7.37 KB
/
main.carp
File metadata and controls
216 lines (201 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
(defmodule Tutorial
(defdynamic next- '())
(defdynamic sections '())
(defdynamic all-sections '())
(defdynamic waiting false)
;; For whatever reason, calling set! here doesn't work, so it's been bumped up to
;; play-section, where it does work \_(ツ)_/.
(doc question
"Add a question to a tutorial section."
""
"Questions prompt a user for input equal to `answer`."
"The prompt displayed is `question`. Users answer questions using `submit`."
""
"```"
"(Tutorial.question \"How old are you?\ 1000)"
"```")
(defndynamic question [question answer]
(fn [] (do (macro-log question)
answer)))
(doc defsection
"Defines a new section for a tutorial."
"Sections are comprised of a name and any number of strings and `question`s."
""
"```"
"(Tutorial.defsection \"Introduction\""
"\"This is a tutorial for Carp. It is meant to help you get started, and help \""
"\"you understand what Carp is about.\""
""
"\"Throughout this tutorial, you will sometimes be asked to write code. To do \""
"\"that, you will have to use the `submit` function. Let’s try it out!\""
""
"(Tutorial.question"
"\"Would you like to continue? If so, enter `(submit true)`\""
"true))"
""
"```")
(defmacro defsection [name :rest contents]
(do
(set! Tutorial.sections
(cons-last (list name contents) Tutorial.sections))
;; Maintain a master list of sections so that we may start any section of the tutorial.
(set! Tutorial.all-sections
(cons-last (list name contents) Tutorial.all-sections))))
;; Returns a listy tuple, containing 1. the answer to a question, if asked, and 2. a bool indicating whether or not
;; we should wait til the next submit call to continue to the next section.
(defndynamic play-section-detail [detail]
(if (list? detail)
(if (empty? detail)
()
(list (detail)
true))
(do (macro-log detail)
(list () false))))
(defndynamic play-section [sections]
(if (empty? sections)
(do (macro-log "You've reached the end of the tutorial, congrats!")
())
(do (macro-log "# " (car (car sections)))
(macro-log "")
(macro-log "")
(let [res (last (map Tutorial.play-section-detail (cadr (car sections))))]
(if (cadr res)
(do
(set! Tutorial.sections (cdr sections))
(set! Tutorial.waiting true)
(set! Tutorial.next- (car res)))
(Tutorial.play (cdr sections))))))) ;; no wait, advance to the next section
(doc play
"Runs a tutorial from start to finish, keeping track of question state.")
(defndynamic play [sections]
(Tutorial.play-section sections))
)
(Tutorial.defsection "Introduction"
"This is a tutorial for Carp. It is meant to help you get started, and help "
"you understand what Carp is about."
""
"Throughout this tutorial, you will sometimes be asked to write code. To do "
"that, you will have to use the `submit` function. Let’s try it out!"
""
(Tutorial.question
"Would you like to continue? If so, enter `(submit true)`!"
true))
(Tutorial.defsection "Values"
"Carp is a strongly-typed language, which means that the types of values "
"matter, and that variables—we’ll talk about those next—cannot change their "
"type on the fly."
""
"So, let’s look at some types!"
""
"```"
"; first off: comments start with a semicolon!"
""
"; this is an `Int` type, or an integer"
"1"
""
"; this is an `Long` type, or a bigger integer. Usually"
"; an `Int` is enough, so we made it a little more convenient to type"
"1l"
""
"; this is a `Double` type, a value with a fraction"
"1.0"
""
"; this is a `Float` type, also a value with a fraction."
"; It is slightly less precise than a `Double`"
"1.0f"
""
"; Text is represented using “strings”, which are always wrapped in double "
"; quotes:"
"\"hi, i’m a string!\""
""
"; If you need multiple values of the same type in a row, you can use an "
"; `Array`"
"[1 2 3]"
"```"
(Tutorial.question
"Can you tell me what an array of the same values as doubles would look like? Remember to use `(submit <your answer>)`!"
[1.0 2.0 3.0]))
(Tutorial.defsection "Structures"
"; Types can also have multiple members. In other languages"
"; this might be called a “struct”."
";"
"; This is how you define them:"
"(deftype Point [x Int, y Int])"
""
"; Now we have a Point, which has an `x` and `y` coordinate,"
"; expressed as integer. We can build one using `init`:"
"(Point.init 0 1)"
(Tutorial.question
"How would a type named `Person`, with a string member `name` and an integer member `age` be defined?"
'(deftype Person [name String, age Int])))
(Tutorial.defsection "To Do"
"This is still to do!")
(doc toc
"Returns a table of contents for a tutorial as a list of "
"index/title pairs, indexed from 0."
""
"```"
"(toc)"
"=> ([0 \"Introduction\"] [1 \"To Do\"])"
"```")
(defndynamic toc []
(let [sections (map car Tutorial.all-sections)
indices (unreduce (curry + 1) -1 (length Tutorial.all-sections) (list))]
(zip (curry (flip collect-into) 'array) indices sections)))
(doc start-tutorial-at
"Starts a tutorial from a given section number."
"Call `toc` to get section numbers."
""
"```"
"(start-tutorial-at 1)"
"=> # To Do"
"...tutorial contents etc."
"(start-tutorial-at 0)"
"=> # Introduction"
"... etc."
"```")
(defndynamic start-tutorial-at [section-number]
(let [sections (toc)
index (- section-number 1)]
(do (if (< index 0)
(set! Tutorial.sections Tutorial.all-sections)
(set! Tutorial.sections (eval (nthcdr index Tutorial.all-sections))))
(Tutorial.play Tutorial.sections))))
(doc restart-tutorial
"Starts a tutorial from its first section, regardless of the current state."
""
"```"
"(tutorial)"
"=> # Section One"
"(submit answer)"
"=> # Section Two"
"(restart-tutorial)"
"=> # Section One"
"```")
(defndynamic restart-tutorial []
(do (set! Tutorial.sections Tutorial.all-sections)
(Tutorial.play Tutorial.sections)))
(doc submit
"Submits an answer to a tutorial question.")
(defmacro submit [form]
(if (and (and (list? Tutorial.next-) (= (length Tutorial.next-) 0))
(= Tutorial.waiting false))
(macro-error "No question was asked yet!")
(if (= Tutorial.next- form)
(do
(macro-log "That’s correct!")
(macro-log "Result: " (eval form))
(set! Tutorial.waiting false)
(Tutorial.play Tutorial.sections)) ;; run the next section
(do
(macro-log "That’s not quite right.")
(macro-log "If you are not sure about the answer, type `(reveal)`.")))))
(doc reveal
"Reveals the answer to a tutorial question.")
(defndynamic reveal []
(do
(macro-log "The answer I was looking for was: " Tutorial.next-)
(macro-log "Result: " (eval Tutorial.next-))
(set! Tutorial.waiting false)
(Tutorial.play Tutorial.sections))) ;; run the next section
(defndynamic tutorial [] (Tutorial.play Tutorial.sections))