英文:
Mojo-lang error about fn, recursion factorial code
问题
I'm a student. I am currently doing research about how fast Mojo Lang is. I want to run the recursion code, and compare it to Python. I decided to write a simple recursion code, and an error occurred. Please help me.
I tried to make a factorial code. If I run the code, it prints me a factorial of the num variable.
Here is my code:
fn fact(n: Int):
if n == 1:
return 1
else:
return n * fact(n - 1)
var num = 14
if num < 0:
print("sry")
elif num == 0:
print("1")
else:
print(fact(num))
And this is the error:
error: Expression [19]:7:16: cannot implicitly convert 'Int' value to 'None' in return value
return 1
^
error: Expression [19]:9:22: ambiguous call to 'fact', each candidate requires 0 implicit conversions, disambiguate with an explicit cast
return n * fact(n - 1)
~~~~^~~~~
Expression [19]:5:1: candidate declared here
fn fact(n: Int):
^
Expression [14]:5:1: candidate declared here
def fact(n: Int):
^
error: Expression [19]:29:19: ambiguous call to 'fact', each candidate requires 0 implicit conversions, disambiguate with an explicit cast
print(fact(num))
~~~~^~~~~
Expression [19]:5:1: candidate declared here
fn fact(n: Int):
^
Expression [14]:5:1: candidate declared here
def fact(n: Int):
^
英文:
I'm a student. I am currently doing research about how fast Mojo Lang is. I want to run the recursion code, and compare it to python. I decided to write a simple recursion code, and an error occurred. Please help me.
I tried to make a factorial code. If i run the code, it prints me a factorial of num variable.
here is my code
fn fact(n:Int):
if n==1:
return 1
else:
return n*fact(n-1)
var num = 14
if num<0:
print("sry")
elif num ==0:
print("1")
else:
print(fact(num))
and this is the error
error: Expression [19]:7:16: cannot implicitly convert 'Int' value to 'None' in return value
return 1
^
error: Expression [19]:9:22: ambiguous call to 'fact', each candidate requires 0 implicit conversions, disambiguate with an explicit cast
return n*fact(n-1)
~~~~^~~~~
Expression [19]:5:1: candidate declared here
fn fact(n:Int):
^
Expression [14]:5:1: candidate declared here
def fact(n:Int):
^
error: Expression [19]:29:19: ambiguous call to 'fact', each candidate requires 0 implicit conversions, disambiguate with an explicit cast
print(fact(num))
~~~~^~~~~
Expression [19]:5:1: candidate declared here
fn fact(n:Int):
^
Expression [14]:5:1: candidate declared here
def fact(n:Int):
^
答案1
得分: 1
根据fn的文档:
缺少返回类型说明符会被解释为返回
None而不是未知的返回类型。
因此,您应该为fact函数指定返回类型为Int,因为解释器会假定返回None,并在函数返回Int时发出警告。
fn fact(n:Int) -> Int:
if n==1:
return 1
else:
return n*fact(n-1)
英文:
According the documentation of fn:
> a missing return type specifier is interpreted as returning None instead of an unknown return type.
So you should specify the return type for the fact function as Int, because the interpreter assumes None and complains when the function return an Int.
fn fact(n:Int) -> Int:
if n==1:
return 1
else:
return n*fact(n-1)
答案2
得分: 0
第一个错误消息告诉您问题所在:该函数应返回None,而您尝试返回一个Int。这应该提示您如何修复。
英文:
The first error message tells you the problem: the function is meant to return None and you try to return an Int. This should hint you how to fix.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论