g<- function(x){ # xは条件分岐の信号的なもの
pattern<- 0 # 走者パターンとか
score<- 0 # 得点
switch(x, # switch関数内のxは1以上の整数しか扱ってくれないらしい
(pattern<- 1) && (score<- 0), # x=1なら、こうなる
(pattern<- 2) && (score<- 1), # x=2なら、こうなる
(pattern<- 3) && (score<- 0), # x=3なら、こうなる
(pattern<- 4) && (score<- 2) # x=4なら、こうなる
) # x=0やx=5を返すと、NULL。
print(pattern) # パターン返してくれよ
print(score) # 得点返してくれよ
}
> g(0) # switch関数が働いていないので、g内で最初に定義したやつが返ってくる。
[1] 0
[1] 0
> g(2) # うまくいったウマ--------
[1] 2
[1] 1