質問編集履歴
3
内容の充実化
test
CHANGED
File without changes
|
test
CHANGED
@@ -163,3 +163,121 @@
|
|
163
163
|
assoc 4 [(2,"two"); (3,"three"); (5,"five")];;
|
164
164
|
|
165
165
|
これならばNoneが返る.
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
###追記
|
174
|
+
|
175
|
+
現状このようになっています.
|
176
|
+
|
177
|
+
しかしまだエラーが出てきます.コードの下にエラーを示します.
|
178
|
+
|
179
|
+
```ocaml
|
180
|
+
|
181
|
+
type exp =
|
182
|
+
|
183
|
+
| Num of int
|
184
|
+
|
185
|
+
| Var of string
|
186
|
+
|
187
|
+
| Add of exp * exp
|
188
|
+
|
189
|
+
| Mul of exp * exp;;
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
let make_sum a1 a2 = Add (a1,a2);;
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
let make_product m1 m2 = Mul (m1,m2);;
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
let rec assoc x ls =
|
202
|
+
|
203
|
+
match ls with
|
204
|
+
|
205
|
+
|[] -> None
|
206
|
+
|
207
|
+
|(a,b)::t -> if a = x then Some b
|
208
|
+
|
209
|
+
else assoc x t;;
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
let calc var =
|
214
|
+
|
215
|
+
match var with
|
216
|
+
|
217
|
+
| Add (Num x, Num y) -> Some (x+y)
|
218
|
+
|
219
|
+
| Mul (Num x, Num y) -> Some (x*y);;
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
let rec eval ls var =
|
224
|
+
|
225
|
+
match var with
|
226
|
+
|
227
|
+
|Num n -> Some n
|
228
|
+
|
229
|
+
|Var x -> assoc x ls
|
230
|
+
|
231
|
+
|Add (x, y) -> calc Add ( eval ls Some x, eval ls Some y )
|
232
|
+
|
233
|
+
|Mul (x, y) -> calc Mul ( eval ls Some x, eval ls Some y );;
|
234
|
+
|
235
|
+
```
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
####エラー
|
240
|
+
|
241
|
+
```ここに言語を入力
|
242
|
+
|
243
|
+
type exp = Num of int | Var of string | Add of exp * exp | Mul of exp * exp
|
244
|
+
|
245
|
+
val make_sum : exp -> exp -> exp = <fun>
|
246
|
+
|
247
|
+
val make_product : exp -> exp -> exp = <fun>
|
248
|
+
|
249
|
+
val assoc : 'a -> ('a * 'b) list -> 'b option = <fun>
|
250
|
+
|
251
|
+
File "ex3_3.ml", line 18, characters 4-98:
|
252
|
+
|
253
|
+
18 | ....match var with
|
254
|
+
|
255
|
+
19 | | Add (Num x, Num y) -> Some (x+y)
|
256
|
+
|
257
|
+
20 | | Mul (Num x, Num y) -> Some (x*y)..
|
258
|
+
|
259
|
+
Warning 8: this pattern-matching is not exhaustive.
|
260
|
+
|
261
|
+
Here is an example of a case that is not matched:
|
262
|
+
|
263
|
+
(Add (Num _, (Var _|Add (_, _)|Mul (_, _)))|
|
264
|
+
|
265
|
+
Add ((Var _|Add (_, _)|Mul (_, _)), _)|
|
266
|
+
|
267
|
+
Mul (Num _, (Var _|Add (_, _)|Mul (_, _)))|
|
268
|
+
|
269
|
+
Mul ((Var _|Add (_, _)|Mul (_, _)), _)|Num _|Var _)
|
270
|
+
|
271
|
+
val calc : exp -> int option = <fun>
|
272
|
+
|
273
|
+
File "ex3_3.ml", line 26, characters 19-23:
|
274
|
+
|
275
|
+
26 | |Add (x, y) -> calc Add ( eval ls Some x, eval ls Some y )
|
276
|
+
|
277
|
+
^^^^
|
278
|
+
|
279
|
+
Error: This function has type exp -> int option
|
280
|
+
|
281
|
+
It is applied to too many arguments; maybe you forgot a `;'.
|
282
|
+
|
283
|
+
```
|
2
情報が足りなかった
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
(例)
|
8
8
|
|
9
|
-
eval [("x",4);("y",3)] (Mul(Mul(x,y),Add(x,Num 3)));;
|
9
|
+
eval [("x",4);("y",3)] (Mul(Mul(Var "x", Var "y"),Add(Var "x",Num 3)));;
|
10
10
|
|
11
11
|
- : int = Some 84
|
12
12
|
|
1
内容の充実化
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
(例)
|
8
8
|
|
9
|
-
eval [(x,4);(y,3)] (Mul(Mul(x,y),Add(x,Num 3)));;
|
9
|
+
eval [("x",4);("y",3)] (Mul(Mul(x,y),Add(x,Num 3)));;
|
10
10
|
|
11
11
|
- : int = Some 84
|
12
12
|
|