Nim
1var ans = int.low 2for i in 0..<10: 3 let n = stdin.readLine.len 4 if n > ans: 5 ans = n
と書くようなところを、
Nim
1let ans = MAX: 2 for i in 0..<10: 3 eval stdin.readLine.len
みたいに書けるようなテンプレートは作れますか?
eval
は MAX文の中でだけ使える予約語みたいな。
continue
とかが for文の中でだけ使えるのと同じようなノリで。
Nim
1template MAX(eval:untyped, body:untyped) :int = 2 proc exec(eval:proc(x:int)) = body 3 var ans = int.low 4 exec do (x:int) -> void: 5 if x > ans: 6 ans = x 7 ans 8 9let ans = MAX eval: 10 for i in 0..<10: 11 eval stdin.readLine.len
これだといけるのですが、
Nim
1template MAX(eval:untyped, body:untyped) :int = 2 proc exec(eval:proc(x:int)) = body 3 var ans = int.low 4 exec do (x:int) -> void: 5 if x > ans: 6 ans = x 7 ans 8 9echo MAX eval: 10 for i in 0..<10: 11 eval stdin.readLine.len
こう書くとコンパイルエラーになってしまいます。
引数が1つ増えちゃうのもちょっと嫌です。
あなたの回答
tips
プレビュー