質問編集履歴
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -43,4 +43,5 @@
|
|
43
43
|
for i in 0..<10:
|
44
44
|
eval stdin.readLine.len
|
45
45
|
```
|
46
|
-
こう書くとコンパイルエラーになってしまいます。
|
46
|
+
こう書くとコンパイルエラーになってしまいます。
|
47
|
+
引数が1つ増えちゃうのもちょっと嫌です。
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -17,13 +17,20 @@
|
|
17
17
|
`continue` とかが for文の中でだけ使えるのと同じようなノリで。
|
18
18
|
|
19
19
|
```Nim
|
20
|
-
template
|
20
|
+
template MAX(eval:untyped, body:untyped) :int =
|
21
|
-
proc exec(eval:proc()) = body
|
21
|
+
proc exec(eval:proc(x:int)) = body
|
22
|
-
var ans =
|
22
|
+
var ans = int.low
|
23
|
-
exec do () -> void:
|
23
|
+
exec do (x:int) -> void:
|
24
|
+
if x > ans:
|
24
|
-
|
25
|
+
ans = x
|
25
26
|
ans
|
26
27
|
|
28
|
+
let ans = MAX eval:
|
29
|
+
for i in 0..<10:
|
30
|
+
eval stdin.readLine.len
|
31
|
+
```
|
32
|
+
これだといけるのですが、
|
33
|
+
```Nim
|
27
34
|
template MAX(eval:untyped, body:untyped) :int =
|
28
35
|
proc exec(eval:proc(x:int)) = body
|
29
36
|
var ans = int.low
|
@@ -31,5 +38,9 @@
|
|
31
38
|
if x > ans:
|
32
39
|
ans = x
|
33
40
|
ans
|
41
|
+
|
42
|
+
echo MAX eval:
|
43
|
+
for i in 0..<10:
|
44
|
+
eval stdin.readLine.len
|
34
45
|
```
|
35
|
-
こ
|
46
|
+
こう書くとコンパイルエラーになってしまいます。
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,4 +14,22 @@
|
|
14
14
|
みたいに書けるようなテンプレートは作れますか?
|
15
15
|
|
16
16
|
`eval` は MAX文の中でだけ使える予約語みたいな。
|
17
|
-
`continue` とかが for文の中でだけ使えるのと同じようなノリで。
|
17
|
+
`continue` とかが for文の中でだけ使えるのと同じようなノリで。
|
18
|
+
|
19
|
+
```Nim
|
20
|
+
template COUNT(eval:untyped, body:untyped) :int =
|
21
|
+
proc exec(eval:proc()) = body
|
22
|
+
var ans = 0
|
23
|
+
exec do () -> void:
|
24
|
+
ans.inc
|
25
|
+
ans
|
26
|
+
|
27
|
+
template MAX(eval:untyped, body:untyped) :int =
|
28
|
+
proc exec(eval:proc(x:int)) = body
|
29
|
+
var ans = int.low
|
30
|
+
exec do (x:int) -> void:
|
31
|
+
if x > ans:
|
32
|
+
ans = x
|
33
|
+
ans
|
34
|
+
```
|
35
|
+
この2つのテンプレートを同時に使うとコンパイルエラーになってしまいます。
|