Mojo-lang关于fn、递归阶乘代码的错误

huangapple go评论97阅读模式
英文:

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&lt;0:
    print(&quot;sry&quot;)
elif num ==0:
    print(&quot;1&quot;)
else:
    print(fact(num))

and this is the error

error: Expression [19]:7:16: cannot implicitly convert &#39;Int&#39; value to &#39;None&#39; in return value
        return 1
               ^

error: Expression [19]:9:22: ambiguous call to &#39;fact&#39;, 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 &#39;fact&#39;, 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) -&gt; 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.

huangapple
  • 本文由 发表于 2023年6月13日 16:14:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定