允许针对不同参数值实现单独函数的功能的名称是什么? [英] What is the name of feature that allows separate function implementations for different values of parameters?
问题描述
在某些编程语言中,可以为特定的参数值指定函数实现:
In some programming languages one can specify the function implementation for particular values of arguments:
factorial[x] = x * factorial[x-1]
factorial[0] = 1
使用 0
调用函数将使用第二个定义,而使用 3
调用将使用第一个定义.
Calling the function with 0
will use the second definition while calling it with 3
will use the first definition.
我知道这是一种函数重载,但是如果我要搜索具有函数重载的语言,我会发现很多没有此功能而仅根据数量或类型进行重载的语言.有更具体的名称吗?
I am aware that this is a kind of function overloading, but if I would search for languages with function overloading, I will find a lot that don't have this feature but only overload depending on arity or types. Is there a more specific name for this?
推荐答案
此类 case-by-case 函数 在数学中被称为 分段函数.例如,在 Scala 中,它们可以使用 case
这样的语句来实现
Such case-by-case functions are known as piecewise functions in mathematics. For example, in Scala they can be implemented using case
statements like so
val factorial: Int => Int = {
case 0 => 1
case x => x * factorial(x - 1)
}
哪个输出
factorial(4) // res1: Int = 24
这是一种使用模式匹配的函数定义形式.
This is a form of function definition using pattern matching.
同样,在 Haskell 中,我们可以像这样使用模式匹配定义一个函数
Similarly, in Haskell we can define a function using pattern matching like so
factorial :: Integer -> Integer
factorial 0 = 1
factorial x = x * factorial (x - 1)
这篇关于允许针对不同参数值实现单独函数的功能的名称是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!