函数式编程中如何存在时间函数? [英] How can a time function exist in functional programming?

查看:135
本文介绍了函数式编程中如何存在时间函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不承认,我对函数式编程知之甚少。我从这里到那里读了它,所以知道在函数式编程中,无论调用函数多少次,函数都会返回相同的输出。它就像数学函数一样,对于涉及函数表达式的输入参数的相同值,计算相同的输出。



例如,考虑这个:

  f(x,y )= x * x + y; //它是一个数学函数

无论你使用多少次 f (10,4),其值将始终为 104 。因此,无论你写了 f(10,4),你都可以用 104 代替它,而不会改变整个表达式的价值。此属性被称为表达式的参照透明度



正如维基百科所说(链接),

$相反,在函数式代码中,函数的输出值仅依赖于输入到函数中的参数,所以使用相同的函数调用函数f两次参数x的值将两次产生相同的结果f(x)。

所以我的问题是:函数式编程中是否可以使用时间函数(它返回 current 时间) ?


  • 如果是的话,那么它如何存在?它不违反函数式编程的原则吗?它特别违反了参照透明度,这是函数式编程的一个属性(如果我没有错误理解它)。

  • 如果不是,那么如何知道函数式编程中的当前时间?

解决方案

另一种解释方法是:no function 可以获取当前时间(因为它不断变化),但是动作可以获取当前时间。假设 getClockTime 是一个表示获取当前时间的动作的常量(或者是一个无函数函数)。这个 action 每次都是一样的,不管它什么时候使用,所以它是一个真正的常量。同样,比方说, print 是一个函数,它需要一些时间表示并将其打印到控制台。由于函数调用不能在纯粹的函数式语言中产生副作用,我们可以想象它是一个函数,它需要一个时间戳并返回将其打印到控制台的动作。再一次,这是一个真正的功能,因为如果你给它一个相同的时间戳,它将每次都返回相同的 action 打印它。



现在,如何将当前时间打印到控制台?那么,你必须结合这两个行动。那么我们该怎么做呢?我们不能将 getClockTime 传递给 print ,因为print需要一个时间戳,而不是一个动作。但我们可以想象,有一个操作符>> = ,它将两个操作组合在一起,一个获得时间戳,以一个作为参数并打印它。将此应用于前面提到的动作,结果是... tadaaa ...获取当前时间并打印它的新动作。这在Haskell中的确如此。

  Prelude> System.Time.getClockTime>> = print 
Fri Sep 2 01:13:23东京(标准时)2011

因此,在概念上,您可以通过这种方式查看它:纯功能程序不执行任何IO操作,它定义了一个操作,然后运行系统执行该操作。 action 每次都是一样的,但是执行的结果取决于执行的时间。

我没有知道这是否比其他解释更清楚,但它有时帮助我以这种方式来思考。

I've to admit that I don't know much about functional programming. I read about it from here and there, and so came to know that in functional programming, a function returns the same output, for same input, no matter how many times the function is called. It's exactly like mathematical function which evaluates to same output for same value of input parameter which involves in the function expression.

For example, consider this:

f(x,y) = x*x + y; //it is a mathematical function

No matter how many times you use f(10,4), its value will always be 104. As such, wherever you've written f(10,4), you can replace it with 104, without altering the value of the whole expression. This property is referred to as referential transparency of an expression.

As Wikipedia says (link),

Conversely, in functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times.

So my question is: can a time function (which returns the current time) exist in functional programming?

  • If yes, then how can it exist? Does it not violate the principle of functional programming? It particularly violates referential transparency which is one of the property of functional programming (if I correctly understand it).

  • Or if no, then how can one know the current time in functional programming?

解决方案

Another way to explain it is this: no function can get the current time (since it keeps changing), but an action can get the current time. Let's say that getClockTime is a constant (or a nullary function, if you like) which represents the action of getting the current time. This action is the same every time no matter when it is used so it is a real constant.

Likewise, let's say print is a function which takes some time representation and prints it to the console. Since function calls cannot have side effects in pure functional language, we instead imagine that it is a function which takes a timestamp and returns the action of printing it to the console. Again, this is a real function, because if you give it the same timestamp, it will return the same action of printing it every time.

Now, how can you print the current time to the console? Well, you have to combine the two actions. So how can we do that? We cannot just pass getClockTime to print, since print expects a timestamp, not an action. But we can imagine that there is an operator, >>=, which combines two actions, one which gets a timestamp, and one which takes one as argument and prints it. Applying this to the actions previously mentioned, the result is... tadaaa... a new action which gets the current time and prints it. And this is incidently exactly how it is done in Haskell.

Prelude> System.Time.getClockTime >>= print
Fri Sep  2 01:13:23 東京 (標準時) 2011

So, conceptually, you can view it in this way: A pure functional program does not perform any IO, it defines an action, which the runtime system then executes. The action is the same every time, but the result of executing it depends on the circumstances of when it is executed.

I don't know if this was any clearer than the other explanations, but it sometimes helps me to think of it this way.

这篇关于函数式编程中如何存在时间函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆