我必须承认我对函数式编程了解不多。我从这里和那里读到它,所以开始知道在函数式编程中,一个函数返回相同的输出,对于相同的输入,无论函数被调用多少次。它就像一个数学函数,对于函数表达式中包含的输入参数的相同值,计算出相同的输出。

例如,考虑这个:

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

不管你用了多少次f(10,4)它的值总是104。因此,无论你在哪里写f(10,4),你都可以用104替换它,而不改变整个表达式的值。此属性称为表达式的引用透明性。

正如维基百科所说,

相反,在函数式代码中,函数的输出值只取决于函数的输入参数,因此调用函数f两次,参数x的值相同,两次将产生相同的结果f(x)。

函数式编程中是否存在时间函数(返回当前时间)?

如果是,那它怎么可能存在?它是否违反了函数式编程的原则?它尤其违反了引用透明性,这是函数式编程的特性之一(如果我理解正确的话)。 如果没有,那么在函数式编程中如何知道当前时间呢?


当前回答

您正在讨论函数式编程中一个非常重要的主题,即执行I/O。许多纯语言是通过使用嵌入式领域特定语言来实现的,例如,一种子语言,其任务是编码可以产生结果的动作。

例如,Haskell运行时希望我定义一个名为main的操作,该操作由组成程序的所有操作组成。运行时然后执行此操作。大多数情况下,这样做只会执行纯代码。运行时将不时使用计算出的数据执行I/O,并将数据反馈回纯代码。

You might complain that this sounds like cheating, and in a way it is: by defining actions and expecting the runtime to execute them, the programmer can do everything a normal program can do. But Haskell's strong type system creates a strong barrier between pure and "impure" parts of the program: you cannot simply add, say, two seconds to the current CPU time, and print it, you have to define an action that results in the current CPU time, and pass the result on to another action that adds two seconds and prints the result. Writing too much of a program is considered bad style though, because it makes it hard to infer which effects are caused, compared to Haskell types that tell us everything we can know about what a value is.

示例:clock_t c = time(NULL);Printf ("%d\n", c + 2);在Haskell中,vs. main = getCPUTime >>= \ C -> print (C + 2*1000*1000*1000*1000)操作符>>=用于组合动作,将第一个动作的结果传递给产生第二个动作的函数。这看起来很神秘,Haskell编译器支持语法糖,允许我们编写后面的代码如下:

type Clock = Integer -- To make it more similar to the C code

-- An action that returns nothing, but might do something
main :: IO ()
main = do
    -- An action that returns an Integer, which we view as CPU Clock values
    c <- getCPUTime :: IO Clock
    -- An action that prints data, but returns nothing
    print (c + 2*1000*1000*1000*1000) :: IO ()

后者看起来很有必要,不是吗?

其他回答

您正在讨论函数式编程中一个非常重要的主题,即执行I/O。许多纯语言是通过使用嵌入式领域特定语言来实现的,例如,一种子语言,其任务是编码可以产生结果的动作。

例如,Haskell运行时希望我定义一个名为main的操作,该操作由组成程序的所有操作组成。运行时然后执行此操作。大多数情况下,这样做只会执行纯代码。运行时将不时使用计算出的数据执行I/O,并将数据反馈回纯代码。

You might complain that this sounds like cheating, and in a way it is: by defining actions and expecting the runtime to execute them, the programmer can do everything a normal program can do. But Haskell's strong type system creates a strong barrier between pure and "impure" parts of the program: you cannot simply add, say, two seconds to the current CPU time, and print it, you have to define an action that results in the current CPU time, and pass the result on to another action that adds two seconds and prints the result. Writing too much of a program is considered bad style though, because it makes it hard to infer which effects are caused, compared to Haskell types that tell us everything we can know about what a value is.

示例:clock_t c = time(NULL);Printf ("%d\n", c + 2);在Haskell中,vs. main = getCPUTime >>= \ C -> print (C + 2*1000*1000*1000*1000)操作符>>=用于组合动作,将第一个动作的结果传递给产生第二个动作的函数。这看起来很神秘,Haskell编译器支持语法糖,允许我们编写后面的代码如下:

type Clock = Integer -- To make it more similar to the C code

-- An action that returns nothing, but might do something
main :: IO ()
main = do
    -- An action that returns an Integer, which we view as CPU Clock values
    c <- getCPUTime :: IO Clock
    -- An action that prints data, but returns nothing
    print (c + 2*1000*1000*1000*1000) :: IO ()

后者看起来很有必要,不是吗?

大多数函数式编程语言都不是纯粹的,也就是说,它们允许函数不仅依赖于它们的值。在这些语言中,完全有可能有一个返回当前时间的函数。从你标记这个问题的语言中,这适用于Scala和f#(以及ML的大多数其他变体)。

在Haskell和Clean等纯语言中,情况就不同了。在Haskell中,当前时间不是通过函数,而是通过所谓的IO操作,这是Haskell封装副作用的方式。

在Clean中,它将是一个函数,但该函数将以一个世界值作为参数,并返回一个新的世界值(除了当前时间)作为结果。类型系统将确保每个世界值只能使用一次(并且每个消耗世界值的函数将产生一个新的世界值)。这样,每次调用time函数时都必须使用不同的参数,因此允许每次返回不同的时间。

如果是,那它怎么可能存在?这难道不违反原则吗 函数式编程吗?它尤其违反了参照原则 透明度

它的存在不是纯粹的功能性的。

如果不是,那么如何知道当前时间是泛函的 编程吗?

首先,了解如何在计算机上检索时间可能是有用的。从本质上讲,有一个板载电路来记录时间(这就是电脑通常需要一个小电池的原因)。然后可能会有一些内部进程在某个内存寄存器上设置时间的值。这本质上可以归结为CPU可以检索到的值。


对于Haskell,有一个“IO动作”的概念,它表示一种类型,可以执行一些IO进程。因此,我们不引用时间值,而是引用IO时间值。所有这些都是纯功能性的。我们引用的不是时间,而是“读取时间寄存器的值”之类的东西。

当我们实际执行Haskell程序时,IO操作将实际发生。

我很惊讶,没有一个答案或评论提到共代数或共归纳。通常,同归纳法在对无限数据结构进行推理时被提及,但它也适用于无穷无尽的观察流,例如CPU上的时间寄存器。一个协代数模型隐藏状态;同感应模型观察到这种状态。(正常诱导模型构造状态。)

这是响应式函数式编程中的一个热门话题。如果你对这类东西感兴趣,请阅读以下内容:http://digitalcommons.ohsu.edu/csetech/91/(28页)

Richard B. Kieburtz,“响应式函数式编程”(1997)。CSETech。第91篇(链接)

这完全可以用纯功能的方式来完成。有几种方法可以做到这一点,但最简单的方法是让time函数不仅返回时间,还返回您必须调用以获得下一次时间测量的函数。

在c#中,你可以这样实现它:

// Exposes mutable time as immutable time (poorly, to illustrate by example)
// Although the insides are mutable, the exposed surface is immutable.
public class ClockStamp {
    public static readonly ClockStamp ProgramStartTime = new ClockStamp();
    public readonly DateTime Time;
    private ClockStamp _next;

    private ClockStamp() {
        this.Time = DateTime.Now;
    }
    public ClockStamp NextMeasurement() {
        if (this._next == null) this._next = new ClockStamp();
        return this._next;
    }
}

(请记住,这是一个简单的示例,而不是实际的示例。特别是,列表节点不能被垃圾收集,因为它们是由ProgramStartTime根的。)

这个“ClockStamp”类就像一个不可变的链表,但实际上节点是按需生成的,所以它们可以包含“当前”时间。任何想要测量时间的函数都应该有一个'clockStamp'参数,并且必须在其结果中返回其最近的时间测量值(这样调用者就不会看到旧的测量值),如下所示:

// Immutable. A result accompanied by a clockstamp
public struct TimeStampedValue<T> {
    public readonly ClockStamp Time;
    public readonly T Value;
    public TimeStampedValue(ClockStamp time, T value) {
        this.Time = time;
        this.Value = value;
    }
}

// Times an empty loop.
public static TimeStampedValue<TimeSpan> TimeALoop(ClockStamp lastMeasurement) {
    var start = lastMeasurement.NextMeasurement();
    for (var i = 0; i < 10000000; i++) {
    }
    var end = start.NextMeasurement();
    var duration = end.Time - start.Time;
    return new TimeStampedValue<TimeSpan>(end, duration);
}

public static void Main(String[] args) {
    var clock = ClockStamp.ProgramStartTime;
    var r = TimeALoop(clock);
    var duration = r.Value; //the result
    clock = r.Time; //must now use returned clock, to avoid seeing old measurements
}

当然,这有点不方便,必须把最后的测量输入输出,输入输出,输入输出。隐藏样板文件的方法有很多,尤其是在语言设计级别。我认为Haskell使用这种技巧,然后通过使用单子隐藏丑陋的部分。