[英]F# recursive function exit?
I am very new to F# programming. 我对F#编程很新。 I am looking for an answer as to why the following recursive function will exit when the n=0 condition is reached.
我正在寻找一个答案,为什么当达到n = 0条件时,下面的递归函数将退出。 Does the "then 1" syntax have a special meaning that equates to EXIT?
“then 1”语法是否具有等同于EXIT的特殊含义?
let rec factorial n =
if n = 0
then 1
else n * factorial (n - 1)
Functional programming languages are expression oriented , as opposed to statement oriented. 函数式编程语言是面向表达式的 ,而不是面向语句的。 This means that everything is an expression that can be evaluated into a value.
这意味着一切都是可以计算为值的表达式。
The control flow structures, if
and match
, are also just expressions. 控制流结构
if
和match
也只是表达式。 The compiler checks that all branches of these expressions return the same type. 编译器检查这些表达式的所有分支是否返回相同的类型。
If you're familiar with the conditional ternary operator in other languages, F#'s expression if true then 1 else 0
, is equivalent to true ? 1 : 0
如果你熟悉其他语言中的条件三元运算符,那么F#的表达式
if true then 1 else 0
,则等于为true ? 1 : 0
true ? 1 : 0
. true ? 1 : 0
。
Furthermore, the function doesn't "exit" as such. 此外,该功能不会“退出”。 It completes evaluation.
它完成了评估。 Each instance of the function (there will be
n + 1
instances) completes evaluation at the end of the if
/ then
/ else
expression. 函数的每个实例(将有
n + 1
实例)在if
/ then
/ else
表达式的末尾完成评估。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.