R(斐波那契序列)中的递归错误
所以我试着自己学习R,只是通过在线教程学习。我正在尝试编写一个递归函数,该函数打印斐波那契序列的前n个项,并且无法使代码在没有错误的情况下运行:R(斐波那契序列)中的递归错误,r,stdin,rscript,R,Stdin,Rscript,所以我试着自己学习R,只是通过在线教程学习。我正在尝试编写一个递归函数,该函数打印斐波那契序列的前n个项,并且无法使代码在没有错误的情况下运行: if错误(nterms代码工作正常,但不应按原样将其输入终端。我的建议是:将代码放入脚本文件(ending.R)中,并对其进行源代码转换(使用?source获取帮助,但实际上非常简单) 在R-Studio中,您只需点击源代码按钮。即可使用Rscriptreplace nterms=as.integer(readline(prompt=“多少个术语?”)
if错误(nterms代码工作正常,但不应按原样将其输入终端。我的建议是:将代码放入脚本文件(ending.R)中,并对其进行源代码转换(使用
?source
获取帮助,但实际上非常简单)
在R-Studio中,您只需点击源代码按钮。即可使用
Rscript
replace
nterms=as.integer(readline(prompt=“多少个术语?”)
与
然后可以将其作为Rscript fib.R
运行,假设代码位于当前工作目录中的文件fib.R
否则,使用R shell中的source(“fib.R”)
执行它
Rscript不在交互模式下运行,也不希望从终端获得任何输入。请检查interactive()
在这两种情况下返回的内容。Rscript将返回FALSE,因为它是非交互的,但在R shell中运行时(使用source()
)该函数将为true
?readline
提到它不能在非交互模式下使用。而readLines
明确连接到stdin。这是非交互模式下readline
的问题。readline
不等待按键并立即执行下一条指令。下面的解决方案是n张贴在
我在下面发布了一个完整的答案,对Fibonnaci数字函数进行了一些修改
recurse_fibonacci <- function(n) {
# Define the initial two values of the sequence
if (n <= 1){
n
} else{
# define the rest of the terms of the sequence using recursion
Recall(n - 1) + Recall(n - 2)
}
}
#Take input from the user
cat("How many terms?\n")
repeat{
nterms <- scan("stdin", what = character(), n = 1)
if(nchar(nterms) > 0) break
}
nterms <- as.integer(nterms)
# check to see if the number of terms entered is valid
if(nterms <= 0) {
print("please enter a positive integer")
} else {
# This part actually calculates and displays the first n terms of the sequence
print("Fibonacci Sequence: ")
for(i in 0:(nterms - 1)){
print(recurse_fibonacci(i))
}
}
抱歉,apr92,但我无法复制任何问题。该示例在我的系统上运行良好。如果我将他的整个代码块复制粘贴到我的控制台中,我会收到类似的错误,可能是因为
readline
命令后立即粘贴的内容被解释为(无效)nterms
。op提到的错误只有在您将相关代码复制并粘贴到终端时才会发生。@apr92如果您只是从RStudio环境或任何文本编辑器运行此代码,而不是将其复制粘贴到命令行或控制台中,它应该可以工作。将代码保存在文件fib.R
,然后从R shell执行此操作<代码>源代码(“fib.R”)感谢您的帮助。错误消失了,但是现在出现了一个新问题:在控制台中的脚本下面,它确实显示了所有代码,在cat()之后它确实要求我输入一个数字,但它只是转到重复循环,似乎从未离开过,我很确定这意味着存在某种无限循环。就像我说的,我在记事本中键入所有内容,然后在RStudio中运行,不知道这是否有区别。我对此非常陌生,非常抱歉更新:点击source按钮只会显示消息“多少术语?”但当我点击8时,它不会打印sequence@apr92请尝试删除代码行repeat
和if(nchar(…)
。好的,我成功了,但我确实需要更改代码中的一些内容。而不是使用recall()和您一样,我坚持使用return。我将nterms=as.integer(readline(“stdin”,n=1))更改为nterms=as.integer(readline()),并且我将for循环中的计数器从0:(nterms-1)更改为0:(nterms),现在它可以工作了。我确实对此有一些问题,比如为什么选择使用cat()而不是readline(),为什么选择使用recal而不是return,在cat(“多少条款?\n”)中,为什么有“\n”?谢谢again@apr921)cat
提供了更好的输出,没有[1]
。最后一个\n
是换行符。2) 使用scan
代替readline
,因为R会自动向readline
发送一个空的”
字符串,而无需等待用户输入并执行下一条指令scan
可以从stdin
读取,并且有一个参数n
,您可以在该参数中传递要读取的输入值的数量。那么您说在交互模式下我不能使用readlines吗?@apr92在交互模式下,您可以使用readlines
。检查?读线
cat ("How many terms?")
nterms = as.integer (readLines ("stdin", n = 1))
recurse_fibonacci <- function(n) {
# Define the initial two values of the sequence
if (n <= 1){
n
} else{
# define the rest of the terms of the sequence using recursion
Recall(n - 1) + Recall(n - 2)
}
}
#Take input from the user
cat("How many terms?\n")
repeat{
nterms <- scan("stdin", what = character(), n = 1)
if(nchar(nterms) > 0) break
}
nterms <- as.integer(nterms)
# check to see if the number of terms entered is valid
if(nterms <= 0) {
print("please enter a positive integer")
} else {
# This part actually calculates and displays the first n terms of the sequence
print("Fibonacci Sequence: ")
for(i in 0:(nterms - 1)){
print(recurse_fibonacci(i))
}
}
rui@rui:~$ Rscript fib.R
How many terms?
8
Read 1 item
[1] "Fibonacci Sequence: "
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
rui@rui:~$