What is the difference between functions in math and functions in programming?
数学中的函数和编程中的函数有什么区别?
6 个解决方案
#1
21
In functional programming you have Referential Transparency, which means that you can replace a function with its value without altering the program. This is true in Math too, but this is not always true in Imperative languages.
在函数式编程中,您具有引用透明性,这意味着您可以在不改变程序的情况下用函数的值替换函数。数学也是如此,但在命令式语言中并非总是如此。
A math function is defined by: a relationship that maps elements from one set (A) to another (B), mapping each element of the first set with only one of the other set. In C (as in other programming languages) this is also true, you have your input set, and your output set (which is almost always only ONE).
一个数学函数被定义为:一个关系映射元素从一组(A)(B),第一组的每个元素映射只有一个其他的集合。在C(如其他编程语言)这也是真的,你输入集和输出集(几乎总是只有一个)。
The main difference, is, then, that ALWAYS if you call f(x)
in math, you will get the same answer, but if you call f'(x)
in C, the answer may not be the same (to same arguments don't get the same output).( I think this has a bit of false.. If you have two exactly machine in the same status, they will output the same.. but what it tries to say is that a function in non-functional languages may not depend solely on the arguments you give them, but on other things of the program)
主要的区别是,如果你在数学中调用f(x),你会得到相同的答案,但是如果你在C中调用f'(x),答案可能不会是相同的(对于相同的参数,不会得到相同的输出)。我认为这有一点不实。如果你有两台机器处于同一状态,它们就会输出相同的东西。但它试图说明的是,非函数语言中的函数可能不仅仅依赖于你给它们的参数,而是依赖于程序的其他东西)
Another difference between math and C functions, is that in Math you can't make a function that goes from a non-empty set to an empty set (in C this would be: You aren't requiered to always return something with your function). Also, not all function are computable (I don't know if there's something similiar to this in math..). You don't have functions for infinite sets (you have finite memory, so the set of the possible input parameters must be finite), but in math, you can define a function for infinite sets (like f: N -> N) and for uncountable sets (like f: R -> R) (In C you have floating point numbers, but they only represent a reduced set of real numbers, which is finite).
math和C函数之间的另一个不同之处在于,在math中,你不能使一个函数从非空集变成空集(在C中,这将是:你不需要总是用你的函数返回一些东西)。而且,并不是所有的函数都是可计算的(我不知道在数学中是否有类似的东西)。你没有功能无限集(你有有限的内存,所以一组可能的输入参数必须是有限的),但是在数学,你可以定义一个函数无限集(像f:N - > N)和不可数集(像f:- > R)(浮点数在C语言中,但是他们只表示一组减少的实数,是有限的)。
Finally, know that functional programming is the nearest thing to math functions that you have, and, you CAN use C as a functional language (or something like that). Check "Functional C"
最后,要知道函数式编程是最接近数学函数的东西,并且可以使用C作为函数语言(或者类似的东西)。检查C“功能”
Sorry if my english is bad, hope my answer helps you.
对不起,如果我的英语不好,希望我的回答对你有帮助。
Summarizing:
总结:
In C you don't always have Referential Transparency. Your functions may not always give the same output for the same input parameters. You can have Math functions that defined for an infinite set of inputs, but in C functions your input is finite. In C functions you can have functions that returns nothing, but in Math you can't have that (if you have a function that has a non empty input set, you must map each element with one of another set).
在C语言中,并不总是具有引用透明性。对于相同的输入参数,函数可能并不总是给出相同的输出。你可以用数学函数定义无限的输入,但是在C函数中,你的输入是有限的。在C函数中,你可以有不返回任何东西的函数,但是在数学中,你不能有那样的函数(如果你有一个非空输入集的函数,你必须将每个元素映射到另一个集合中)。
#2
6
It depends on the domain (I don't mean the domain of the function, I mean the domain of study) and also possibly the language.
这取决于领域(我不是指函数的域,我指的是研究领域),也可能是语言。
In math, a function has an input that maps to only one output for a given input value (vertical line test, remember). In programming, this might not be strictly the same, depending on where you draw the line between "input" and "function logic."
在数学中,一个函数的输入只映射到给定输入值的一个输出(记住,垂直线测试)。在编程中,这可能不是完全相同的,这取决于您在“输入”和“函数逻辑”之间划的线。
For instance, let's imagine we have a function rand() that reads atmospheric conditions to arrive at a truly random number. Let's also imagine that a calling function takes one integer parameter as a mutiplier of sorts. Is the following a function?:
例如,假设我们有一个函数rand(),它读取大气条件以得到一个真正的随机数。我们还假设一个调用函数使用一个整数参数作为排序的多变量。以下是一个函数吗?
int giveRandAtmosWithMul(int mult)
{
return mult * rand();
}
In the mathematic sense, it probably is not a function if you consider mult as the only input to the problem, but clearly rand() is offering input as well (even though rand() always has the same entry point in machine code).
在数学意义上,如果您认为mult是问题的唯一输入,那么它可能不是一个函数,但是显然rand()也提供了输入(尽管rand()在机器码中始终有相同的入口点)。
As you can see, the differences aren't really objectively definable without some standard protocol that everybody agrees to.
正如你所看到的,如果没有每个人都同意的标准协议,这些差异就不能被客观地定义。
#3
3
I think the most important distinction is that functions in math (and functional programming) can't change state, while (imperative) programming functions can.
我认为最重要的区别是,数学(和函数编程)中的函数不能改变状态,而(命令式)编程函数可以。
#4
2
Other answers are correct - these are two different things. I'll show, on the contrary, they are related. I'll denote programming functions by ->
, mathematical functions by =>
.
其他的答案是正确的——这是两个不同的东西。相反,我要证明它们是相关的。我用->表示编程函数,用=>表示数学函数。
Suppose you have a language that supports exceptions. In that case, you can think of a programming function A -> B
as a mathematical function A => B + E
where "B + E" means either something of type B
, or something of type E
. Your language has two keywords to return from a function, "return" and "throw".
假设您有一种支持异常的语言。在这种情况下,您可以将编程函数a - >b想象成数学函数a => B + E,其中“B + E”的意思是B类型的东西,或者E类型的东西。
Now, you can compose two functions f: A -> B
and g: B -> C
by writing g(f(x))
. This is a function that takes an A and returns C. You can do that in many languages, like Java or Python. Under the hood, if f(x)
throws an exception, g
is not called and the exception is propagated - think about g(1/0)
. The language takes care of that and you don't need to check if the result of f
is an exception. The way to describe that mathematically is although f: A => B+E
and g: B => C+E
, the language "lifts" the function g into B+E => C+E
. g
is now a function that might take an exception, but it will simply propagate it.
现在,你可以写出两个函数f: A -> B和g: B -> C通过写g(f(x))。这是一个取a并返回c的函数,您可以在许多语言中这样做,比如Java或Python。在引擎盖下,如果f(x)抛出异常,则不调用g,并传播异常——考虑g(1/0)。语言负责这一点,您不需要检查f的结果是否是异常。数学上的描述方法是,虽然f: A => B+E和g: B => C+E,但语言将函数g“提升”为B+E => C+E。g现在是一个可能会有异常的函数,但它会简单地传播它。
In other words define g': B+E => C+E
by
换句话说,定义g': B+E => C+E。
/ g(x) if x ∈ B
g'(x)= |
\ x if x ∈ E
Having two functions f: A => B+E
and g': B+E => C+E
you can safely compose them in mathematical sense. And this is what g(f(x))
in programming does, but it has to lift g
into g'
first.
有两个函数f: A => B+E和g': B+E => C+E你可以在数学意义上安全地组合它们。这就是g(f(x))在编程中的作用,但是它必须先把g提出来。
Think about nondeterminism. If you have a function A -> B
that is nondeterministic, then you can describe it mathematically saying this is a function A => Set(B)
where Set(B)
is the set of possible results. For example, if f(1)
might give you 1, 2 or 3, then mathematically f(1) = {1,2,3}
although while programming when asked for f(1)
you'll get only one of these numbers. Now you can compose nondeterministic functions A->B
and B->C
by writing g(f(x))
. The result will be of type C
, but it will be nondeterministic. Mathematically, composing functions A => Set(B)
and B => Set(C)
gives you A => Set(C)
. Although g
takes only one value of B
, you have to lift it to nondeterministic values g': Set(B) => Set(C)
. For example, g'({1,2})
is union of sets g(1)
and g(2)
. So g(f(x))
means you run f
, take set of all possible results and run g
on each of them. There are two layers of nondeterminism, but they are "flattened" into one.
考虑非确定性。如果你有一个函数a -> B是不确定性的,那么你可以用数学方法描述它说这是一个函数a =>集合(B)集合(B)是一组可能的结果。例如,如果f(1)可能给你1、2或3,那么数学上f(1) ={1、2、3},尽管在编程时,当要求f(1)时,你只能得到其中一个数字。现在你可以用g(f(x))来组成非确定性函数A->B和B->C。结果是C类型的,但它是不确定的。数学上,组合函数A =>集合(B) B =>集合(C)得到A =>集合(C)。虽然g只取B的一个值,但您必须将它提升到不确定性值g': Set(B) => Set(C)。例如,g'({1,2})是集合g(1)和g(2)的联合。所以g(f(x))意味着你运行f,取所有可能结果的集合,然后在每个结果上运行g。非决定论有两层,但它们被“扁平”成一层。
Same with global state. If you make every global variable as an argument of a function and the result, you can think there are no global variables, every function takes all global state and the language has to hand over the state when composing. A function A -> B
reading and possibly writing state S
is a function (A,S) => (B,S)
, which can also be written as A => (S => (B,S))
. Writing this formally is more complicated, but it's the same pattern.
相同的全局状态。如果你把每个全局变量作为一个函数的参数和结果,你可以认为没有全局变量,每个函数都有所有的全局状态,语言在编写时必须交出状态。函数A -> B的读取和可能的写入状态S是一个函数(A,S) => (B,S),也可以写成A => (S => (B,S))。写这篇文章比较复杂,但模式是一样的。
Same can be done with input/output, I described that in another SO answer.
输入/输出也可以这样做,我在另一个SO答案中描述过。
The "magic" that allows to compose "effectful" functions is:
使“有效”功能组合的“魔法”是:
- A way to make the type effectful. For example, turn
B
intoB+E
orSet(B)
. I'll denote effectful version of typeX
asF(X)
. - 一种使类型有效的方法。例如,把B变成B+E或集合(B)。我将表示有效的X类型为F(X)。
- A way to lift the functions:
B -> F(C)
intoF(B) -> F(C)
. It allows to compose functionsA -> F(B)
andB -> F(C)
. - 一种提升函数的方法:B -> F(C)到F(B) -> F(C)。它允许组合函数A -> F(B)和B -> F(C)。
- The keyword
return
must turn an ordinary valueA
intoA+E
, or singletonSet(A)
. So it's type must beX -> F(X)
. - 关键字返回必须将普通值A转换为+E或单例集(A)。它的类型必须是X -> F(X)
A structure composed of those three is called a monad. Monads allow to describe those side effects . A monad might also have some specific functions, for example the exception monad has throw
, the nondeterminism monad has fork
, the state monad has get/put
, the IO monad has read/write
etc.
由这三种结构组成的结构称为monad。Monads可以描述这些副作用。monad也可能有一些特定的函数,例如异常monad有抛出,非决定论monad有fork,状态monad有get/put, IO monad有读写等等。
The moral is: If you regard special effects like randomizing, exceptions, nondeterminism, input/output as a part of the result of a function, then every function is referentially transparent and functions in imperative programming are really mathematical functions, but with very strange result types that also describe special effects. This is the approach taken by pure functional languages like Haskell.
它的寓意是:如果你把随机化、异常、非确定性、输入/输出等特殊效果视为一个函数的结果的一部分,那么每个函数都是参照透明的,命令式编程中的函数都是真正的数学函数,但结果类型却非常奇怪,它们也描述了特殊效果。这是像Haskell这样的纯函数语言所采用的方法。
#5
0
In math, functions don't throw exceptions. :)
在数学中,函数不会抛出异常。:)
A function in computer science is a chunk of code that takes inputs, does something and possibly returns outputs, but it can do a host of things between. It can fetch webpages, send emails, play video, whatever.
计算机科学中的一个函数是一段代码,它接受输入,做一些事情,并可能返回输出,但是它可以做很多事情。它可以获取网页、发送电子邮件、播放视频等等。
In math a function is something very specific and nothing else. A function is usually described as a "machine" that takes in inputs and spits out outputs. While computer science functions do take in inputs, and spit out outputs, they don't have to do so with the precise "the same input always yields the same output" that math requires (e.g. bool IsMyApplicationRunningInFullScreen() returns various values with no inputs at all).
在数学中,函数是非常具体的东西,而不是别的东西。函数通常被描述为接收输入并输出输出的“机器”。虽然计算机科学函数确实接受输入,并输出输出输出,但是它们不需要使用数学要求的精确的“相同的输入总是产生相同的输出”(例如bool IsMyApplicationRunningInFullScreen()返回不同的值,根本没有输入)。
#6
0
Mathematical functions are declarative in nature i.e. they always have "what is" descriptions whereas functions in computer science are imperative i.e. they have "how to" descriptions.
数学函数本质上是陈述性的,即它们总是有“是什么”的描述,而计算机科学中的函数则是必须的,即它们有“如何”的描述。
Reference: Structure and interpretation of computer programs.
参考:计算机程序的结构和解释。
#1
21
In functional programming you have Referential Transparency, which means that you can replace a function with its value without altering the program. This is true in Math too, but this is not always true in Imperative languages.
在函数式编程中,您具有引用透明性,这意味着您可以在不改变程序的情况下用函数的值替换函数。数学也是如此,但在命令式语言中并非总是如此。
A math function is defined by: a relationship that maps elements from one set (A) to another (B), mapping each element of the first set with only one of the other set. In C (as in other programming languages) this is also true, you have your input set, and your output set (which is almost always only ONE).
一个数学函数被定义为:一个关系映射元素从一组(A)(B),第一组的每个元素映射只有一个其他的集合。在C(如其他编程语言)这也是真的,你输入集和输出集(几乎总是只有一个)。
The main difference, is, then, that ALWAYS if you call f(x)
in math, you will get the same answer, but if you call f'(x)
in C, the answer may not be the same (to same arguments don't get the same output).( I think this has a bit of false.. If you have two exactly machine in the same status, they will output the same.. but what it tries to say is that a function in non-functional languages may not depend solely on the arguments you give them, but on other things of the program)
主要的区别是,如果你在数学中调用f(x),你会得到相同的答案,但是如果你在C中调用f'(x),答案可能不会是相同的(对于相同的参数,不会得到相同的输出)。我认为这有一点不实。如果你有两台机器处于同一状态,它们就会输出相同的东西。但它试图说明的是,非函数语言中的函数可能不仅仅依赖于你给它们的参数,而是依赖于程序的其他东西)
Another difference between math and C functions, is that in Math you can't make a function that goes from a non-empty set to an empty set (in C this would be: You aren't requiered to always return something with your function). Also, not all function are computable (I don't know if there's something similiar to this in math..). You don't have functions for infinite sets (you have finite memory, so the set of the possible input parameters must be finite), but in math, you can define a function for infinite sets (like f: N -> N) and for uncountable sets (like f: R -> R) (In C you have floating point numbers, but they only represent a reduced set of real numbers, which is finite).
math和C函数之间的另一个不同之处在于,在math中,你不能使一个函数从非空集变成空集(在C中,这将是:你不需要总是用你的函数返回一些东西)。而且,并不是所有的函数都是可计算的(我不知道在数学中是否有类似的东西)。你没有功能无限集(你有有限的内存,所以一组可能的输入参数必须是有限的),但是在数学,你可以定义一个函数无限集(像f:N - > N)和不可数集(像f:- > R)(浮点数在C语言中,但是他们只表示一组减少的实数,是有限的)。
Finally, know that functional programming is the nearest thing to math functions that you have, and, you CAN use C as a functional language (or something like that). Check "Functional C"
最后,要知道函数式编程是最接近数学函数的东西,并且可以使用C作为函数语言(或者类似的东西)。检查C“功能”
Sorry if my english is bad, hope my answer helps you.
对不起,如果我的英语不好,希望我的回答对你有帮助。
Summarizing:
总结:
In C you don't always have Referential Transparency. Your functions may not always give the same output for the same input parameters. You can have Math functions that defined for an infinite set of inputs, but in C functions your input is finite. In C functions you can have functions that returns nothing, but in Math you can't have that (if you have a function that has a non empty input set, you must map each element with one of another set).
在C语言中,并不总是具有引用透明性。对于相同的输入参数,函数可能并不总是给出相同的输出。你可以用数学函数定义无限的输入,但是在C函数中,你的输入是有限的。在C函数中,你可以有不返回任何东西的函数,但是在数学中,你不能有那样的函数(如果你有一个非空输入集的函数,你必须将每个元素映射到另一个集合中)。
#2
6
It depends on the domain (I don't mean the domain of the function, I mean the domain of study) and also possibly the language.
这取决于领域(我不是指函数的域,我指的是研究领域),也可能是语言。
In math, a function has an input that maps to only one output for a given input value (vertical line test, remember). In programming, this might not be strictly the same, depending on where you draw the line between "input" and "function logic."
在数学中,一个函数的输入只映射到给定输入值的一个输出(记住,垂直线测试)。在编程中,这可能不是完全相同的,这取决于您在“输入”和“函数逻辑”之间划的线。
For instance, let's imagine we have a function rand() that reads atmospheric conditions to arrive at a truly random number. Let's also imagine that a calling function takes one integer parameter as a mutiplier of sorts. Is the following a function?:
例如,假设我们有一个函数rand(),它读取大气条件以得到一个真正的随机数。我们还假设一个调用函数使用一个整数参数作为排序的多变量。以下是一个函数吗?
int giveRandAtmosWithMul(int mult)
{
return mult * rand();
}
In the mathematic sense, it probably is not a function if you consider mult as the only input to the problem, but clearly rand() is offering input as well (even though rand() always has the same entry point in machine code).
在数学意义上,如果您认为mult是问题的唯一输入,那么它可能不是一个函数,但是显然rand()也提供了输入(尽管rand()在机器码中始终有相同的入口点)。
As you can see, the differences aren't really objectively definable without some standard protocol that everybody agrees to.
正如你所看到的,如果没有每个人都同意的标准协议,这些差异就不能被客观地定义。
#3
3
I think the most important distinction is that functions in math (and functional programming) can't change state, while (imperative) programming functions can.
我认为最重要的区别是,数学(和函数编程)中的函数不能改变状态,而(命令式)编程函数可以。
#4
2
Other answers are correct - these are two different things. I'll show, on the contrary, they are related. I'll denote programming functions by ->
, mathematical functions by =>
.
其他的答案是正确的——这是两个不同的东西。相反,我要证明它们是相关的。我用->表示编程函数,用=>表示数学函数。
Suppose you have a language that supports exceptions. In that case, you can think of a programming function A -> B
as a mathematical function A => B + E
where "B + E" means either something of type B
, or something of type E
. Your language has two keywords to return from a function, "return" and "throw".
假设您有一种支持异常的语言。在这种情况下,您可以将编程函数a - >b想象成数学函数a => B + E,其中“B + E”的意思是B类型的东西,或者E类型的东西。
Now, you can compose two functions f: A -> B
and g: B -> C
by writing g(f(x))
. This is a function that takes an A and returns C. You can do that in many languages, like Java or Python. Under the hood, if f(x)
throws an exception, g
is not called and the exception is propagated - think about g(1/0)
. The language takes care of that and you don't need to check if the result of f
is an exception. The way to describe that mathematically is although f: A => B+E
and g: B => C+E
, the language "lifts" the function g into B+E => C+E
. g
is now a function that might take an exception, but it will simply propagate it.
现在,你可以写出两个函数f: A -> B和g: B -> C通过写g(f(x))。这是一个取a并返回c的函数,您可以在许多语言中这样做,比如Java或Python。在引擎盖下,如果f(x)抛出异常,则不调用g,并传播异常——考虑g(1/0)。语言负责这一点,您不需要检查f的结果是否是异常。数学上的描述方法是,虽然f: A => B+E和g: B => C+E,但语言将函数g“提升”为B+E => C+E。g现在是一个可能会有异常的函数,但它会简单地传播它。
In other words define g': B+E => C+E
by
换句话说,定义g': B+E => C+E。
/ g(x) if x ∈ B
g'(x)= |
\ x if x ∈ E
Having two functions f: A => B+E
and g': B+E => C+E
you can safely compose them in mathematical sense. And this is what g(f(x))
in programming does, but it has to lift g
into g'
first.
有两个函数f: A => B+E和g': B+E => C+E你可以在数学意义上安全地组合它们。这就是g(f(x))在编程中的作用,但是它必须先把g提出来。
Think about nondeterminism. If you have a function A -> B
that is nondeterministic, then you can describe it mathematically saying this is a function A => Set(B)
where Set(B)
is the set of possible results. For example, if f(1)
might give you 1, 2 or 3, then mathematically f(1) = {1,2,3}
although while programming when asked for f(1)
you'll get only one of these numbers. Now you can compose nondeterministic functions A->B
and B->C
by writing g(f(x))
. The result will be of type C
, but it will be nondeterministic. Mathematically, composing functions A => Set(B)
and B => Set(C)
gives you A => Set(C)
. Although g
takes only one value of B
, you have to lift it to nondeterministic values g': Set(B) => Set(C)
. For example, g'({1,2})
is union of sets g(1)
and g(2)
. So g(f(x))
means you run f
, take set of all possible results and run g
on each of them. There are two layers of nondeterminism, but they are "flattened" into one.
考虑非确定性。如果你有一个函数a -> B是不确定性的,那么你可以用数学方法描述它说这是一个函数a =>集合(B)集合(B)是一组可能的结果。例如,如果f(1)可能给你1、2或3,那么数学上f(1) ={1、2、3},尽管在编程时,当要求f(1)时,你只能得到其中一个数字。现在你可以用g(f(x))来组成非确定性函数A->B和B->C。结果是C类型的,但它是不确定的。数学上,组合函数A =>集合(B) B =>集合(C)得到A =>集合(C)。虽然g只取B的一个值,但您必须将它提升到不确定性值g': Set(B) => Set(C)。例如,g'({1,2})是集合g(1)和g(2)的联合。所以g(f(x))意味着你运行f,取所有可能结果的集合,然后在每个结果上运行g。非决定论有两层,但它们被“扁平”成一层。
Same with global state. If you make every global variable as an argument of a function and the result, you can think there are no global variables, every function takes all global state and the language has to hand over the state when composing. A function A -> B
reading and possibly writing state S
is a function (A,S) => (B,S)
, which can also be written as A => (S => (B,S))
. Writing this formally is more complicated, but it's the same pattern.
相同的全局状态。如果你把每个全局变量作为一个函数的参数和结果,你可以认为没有全局变量,每个函数都有所有的全局状态,语言在编写时必须交出状态。函数A -> B的读取和可能的写入状态S是一个函数(A,S) => (B,S),也可以写成A => (S => (B,S))。写这篇文章比较复杂,但模式是一样的。
Same can be done with input/output, I described that in another SO answer.
输入/输出也可以这样做,我在另一个SO答案中描述过。
The "magic" that allows to compose "effectful" functions is:
使“有效”功能组合的“魔法”是:
- A way to make the type effectful. For example, turn
B
intoB+E
orSet(B)
. I'll denote effectful version of typeX
asF(X)
. - 一种使类型有效的方法。例如,把B变成B+E或集合(B)。我将表示有效的X类型为F(X)。
- A way to lift the functions:
B -> F(C)
intoF(B) -> F(C)
. It allows to compose functionsA -> F(B)
andB -> F(C)
. - 一种提升函数的方法:B -> F(C)到F(B) -> F(C)。它允许组合函数A -> F(B)和B -> F(C)。
- The keyword
return
must turn an ordinary valueA
intoA+E
, or singletonSet(A)
. So it's type must beX -> F(X)
. - 关键字返回必须将普通值A转换为+E或单例集(A)。它的类型必须是X -> F(X)
A structure composed of those three is called a monad. Monads allow to describe those side effects . A monad might also have some specific functions, for example the exception monad has throw
, the nondeterminism monad has fork
, the state monad has get/put
, the IO monad has read/write
etc.
由这三种结构组成的结构称为monad。Monads可以描述这些副作用。monad也可能有一些特定的函数,例如异常monad有抛出,非决定论monad有fork,状态monad有get/put, IO monad有读写等等。
The moral is: If you regard special effects like randomizing, exceptions, nondeterminism, input/output as a part of the result of a function, then every function is referentially transparent and functions in imperative programming are really mathematical functions, but with very strange result types that also describe special effects. This is the approach taken by pure functional languages like Haskell.
它的寓意是:如果你把随机化、异常、非确定性、输入/输出等特殊效果视为一个函数的结果的一部分,那么每个函数都是参照透明的,命令式编程中的函数都是真正的数学函数,但结果类型却非常奇怪,它们也描述了特殊效果。这是像Haskell这样的纯函数语言所采用的方法。
#5
0
In math, functions don't throw exceptions. :)
在数学中,函数不会抛出异常。:)
A function in computer science is a chunk of code that takes inputs, does something and possibly returns outputs, but it can do a host of things between. It can fetch webpages, send emails, play video, whatever.
计算机科学中的一个函数是一段代码,它接受输入,做一些事情,并可能返回输出,但是它可以做很多事情。它可以获取网页、发送电子邮件、播放视频等等。
In math a function is something very specific and nothing else. A function is usually described as a "machine" that takes in inputs and spits out outputs. While computer science functions do take in inputs, and spit out outputs, they don't have to do so with the precise "the same input always yields the same output" that math requires (e.g. bool IsMyApplicationRunningInFullScreen() returns various values with no inputs at all).
在数学中,函数是非常具体的东西,而不是别的东西。函数通常被描述为接收输入并输出输出的“机器”。虽然计算机科学函数确实接受输入,并输出输出输出,但是它们不需要使用数学要求的精确的“相同的输入总是产生相同的输出”(例如bool IsMyApplicationRunningInFullScreen()返回不同的值,根本没有输入)。
#6
0
Mathematical functions are declarative in nature i.e. they always have "what is" descriptions whereas functions in computer science are imperative i.e. they have "how to" descriptions.
数学函数本质上是陈述性的,即它们总是有“是什么”的描述,而计算机科学中的函数则是必须的,即它们有“如何”的描述。
Reference: Structure and interpretation of computer programs.
参考:计算机程序的结构和解释。