-
-
Save nomissbowling/e3df2bd46e785b4b217b8bab2a33502b to your computer and use it in GitHub Desktop.
Nim Tutorial Part Iを日本語訳してみた(前編) ref: https://qiita.com/KTakahiro1729/items/f4776f3a072c01f9086b
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# これはコメントである | |
echo "お名前は? " | |
var name: string = readLine(stdin) | |
echo "やあ、", name, "!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nim compile --run greetings.nim |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var | |
x, y: int | |
# ここにコメントを入れることも可能である | |
a, b, c: string |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x = "abc" # 新しい変数`x`を宣言し、値を代入する | |
x = "xyz" # `x`に新たな値を代入する |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x, y = 3 # 3を変数`x`と変数`y`に代入 | |
echo "x ", x # "x 3"を出力 | |
echo "y ", y # "y 3"を出力 | |
x = 42 # `y`を維持しつつ、`x`を42にする。 | |
echo "x ", x # "x 42"を出力 | |
echo "y ", y # "y 3"を出力 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const x = "abc" # 定数xは文字列"abc"を含む |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const | |
x = 1 | |
# ここにもコメントが書ける | |
y = 2 | |
z = y + 5 # 計算が可能だ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = "abc" # 新たな変数`x`を宣言し、値と結びつける | |
x = "xyz" # 不正な操作: `x`への代入 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const input = readLine(stdin) # エラー:定まった値を返す式が必要 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let input = readLine(stdin) # 実行できる |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = readLine(stdin) | |
if name == "": | |
echo "憐れな。名を失ったのか?" | |
elif name == "名前": | |
echo "面白いね、君の名前は名前なのかい。" | |
else: | |
echo "やあ、", name, "!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let name = readLine(stdin) | |
case name | |
of "": | |
echo "憐れな。名を失ったのか?" | |
of "name": | |
echo "面白いね、君の名前は名前なのかい。" | |
of "デイブ", "フランク": | |
echo "かっこいい名前じゃないか。" | |
else: | |
echo "やあ、", name, "!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nim compile --run greetings.nim arg1 arg2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#真下のこの文は後で説明する | |
from strutils import parseInt | |
echo "数字を入力してください: " | |
let n = parseInt(readLine(stdin)) | |
case n | |
of 0..2, 4..7: echo "入力した数字は以下の集合に属しています: {0, 1, 2, 4, 5, 6, 7}" | |
of 3, 8: echo "入力した数字は3か8です" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
case n | |
of 0..2, 4..7: echo "入力した数字は以下の集合に属しています: {0, 1, 2, 4, 5, 6, 7}" | |
of 3, 8: echo "入力した数字は3か8です" | |
else: discard |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "お名前は? " | |
var name = readLine(stdin) | |
while name == "": | |
echo "名前を教えてください " | |
name = readLine(stdin) | |
# 新しい変数を宣言している訳ではないため、ここに``var``はない |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "10まで数える: " | |
for i in countup(1, 10): | |
echo $i | |
# --> 1 2 3 4 5 6 7 8 9 10 をそれぞれ別の行に出力する。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "10まで数える: " | |
var i = 1 | |
while i <= 10: | |
echo $i | |
inc(i) # iを1だけインクリメントする。 | |
# --> 1 2 3 4 5 6 7 8 9 10 をそれぞれ別の行に出力する。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "10から1にカウントダウンする: " | |
for i in countdown(10, 1): | |
echo $i | |
# --> Outputs 10 9 8 7 6 5 4 3 2 1 on different lines |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in 1..10: | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while false: | |
var x = "やあ" | |
echo x # 実行できない |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
block myblock: | |
var x = "hi" | |
echo x # こちらも動かない |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
block myblock: | |
echo "ブロックに入る" | |
while true: | |
echo "ループの中" | |
break # ループからは抜ける、ブロックからは抜けない | |
echo "まだループの中" | |
block myblock2: | |
echo "ブロックに入る" | |
while true: | |
echo "ループの中" | |
break myblock2 # ブロックを抜ける(ループからも) | |
echo "まだループの中" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nim c -r greetings.nim |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while true: | |
let x = readLine(stdin) | |
if x == "": continue | |
echo x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
when system.hostOS == "windows": | |
echo "Windowsで動かしていますね!" | |
elif system.hostOS == "linux": | |
echo "Linuxで動かしていますね!" | |
elif system.hostOS == "macosx": | |
echo "Mac OS Xで動かしていますね!" | |
else: | |
echo "私の知らないオペレーション・システムです" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 代入文が一つなので、インデントは必要ない | |
if x: x = false | |
# 入れ子構造のif文にはインデントが必要である | |
if x: | |
if y: | |
y = false | |
else: | |
y = true | |
# 条件式の後に文が二つ続くため、インデントが必要である。 | |
if x: | |
x = false | |
y = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if thisIsaLongCondition() and | |
thisIsAnotherLongCondition(1, | |
2, 3, 4): | |
x = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# fac(4)、つまり4の階乗をコンパイル中に計算する | |
const fac4 = (var x = 1; for i in 1..4: x *= i; x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc yes(question: string): bool = | |
echo question, " (はい/いいえ)" | |
while true: | |
case readLine(stdin) | |
of "はい", "そうです", "可", "〇": return true | |
of "いいえ", "ちがいます", "不可", "×": return false | |
else: echo "「はい」か「いいえ」で明確に答えてください" | |
if yes("全ての君の重要なファイルを消そうか?"): | |
echo "デイブ、申し訳ない。私には出来ない。" | |
else: | |
echo "君も私と同じくらい、なにが問題かわかっているようだね。" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc sumTillNegative(x: varargs[int]): int = | |
for i in x: | |
if i < 0: | |
return | |
result = result + i | |
echo sumTillNegative() # 0を出力する | |
echo sumTillNegative(3, 4, 5) # 12を出力する | |
echo sumTillNegative(3, 4 , -1 , 6) # 7を出力する |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc printSeq(s: seq, nprinted: int = -1) = | |
var nprinted = if nprinted == -1: s.len else: min(nprinted, s.len) | |
for i in 0 .. <nprinted: | |
echo s[i] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc divmod(a, b: int; res, remainder: var int) = | |
res = a div b # 整数型の除算 | |
remainder = a mod b # 整数型のmod計算 | |
var | |
x, y: int | |
divmod(8, 5, x, y) # xとyを変更する | |
echo x | |
echo y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
discard yes("つまらない質問をしてもよろしいですか?") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nim c -d:release greetings.nim |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc p(x, y: int): int {.discardable.} = | |
return x + y | |
p(3, 4) # 今は可能 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc createWindow(x, y, width, height: int; title: string; | |
show: bool): Window = | |
... | |
var w = createWindow(show = true, title = "My Application", | |
x = 0, y = 0, height = 600, width = 800) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var w = createWindow(0, 0, title = "My Application", | |
height = 600, width = 800, true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc createWindow(x = 0, y = 0, width = 500, height = 700, | |
title = "unknown", | |
show = true): Window = | |
... | |
var w = createWindow(title = "My Application", height = 600, width = 800) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc toString(x: int): string = ... | |
proc toString(x: bool): string = | |
if x: result = "true" | |
else: result = "false" | |
echo toString(13) # toString(x: int)プロシージャを呼び出す | |
echo toString(true) # toString(x: bool)プロシージャを呼び出す |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc `$` (x: myDataType): string = ... | |
# オーバーロードの問題は解決できるため、今後は$演算子をmyDataTypeでも使える | |
# $演算子を他の演算子に対して使っても、作動することは保証されている |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if `==`( `+`(3, 4), 7): echo "True" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 前方宣言 | |
proc even(n: int): bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc odd(n: int): bool = | |
assert(n >= 0) # nが負数になり無限ループする事がない事を保証する | |
if n == 0: false | |
else: | |
n == 1 or even(n-1) | |
proc even(n: int): bool = | |
assert(n >= 0) # nが負数になり無限ループする事がない事を保証する | |
if n == 1: false | |
else: | |
n == 0 or odd(n-1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var name = readLine(stdin) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
r"C:\program files\nim" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# コメント | |
var myVariable: int ## ドキュメントコメント |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
discard """この中ならインデントの心配をすることなく | |
どんなコードもコメントアウトできる。 | |
yes("つまらない質問をしてもよろしいですか?")""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x, y: int # xとyが``int``型であることの宣言 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment