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

例如,考虑这个:

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 ()

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

其他回答

你的问题合并了计算机语言的两种相关衡量标准:函数式/命令式和纯粹/不纯粹。

函数式语言定义了函数的输入和输出之间的关系,命令式语言按照特定的执行顺序描述了特定的操作。

纯粹的语言不会产生或依赖副作用,而不纯粹的语言则自始至终都在使用副作用。

百分之百纯粹的程序基本上是无用的。他们可能会进行有趣的计算,但因为他们没有副作用,他们没有输入或输出,所以你永远不会知道他们计算了什么。

要想有用,一个程序至少要有一点不纯。使纯程序有用的一种方法是将它放在一个薄的非纯包装器中。比如下面这个未经测试的Haskell程序:

-- this is a pure function, written in functional style.
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

-- This is an impure wrapper around the pure function, written in imperative style
-- It depends on inputs and produces outputs.
main = do
    putStrLn "Please enter the input parameter"
    inputStr <- readLine
    putStrLn "Starting time:"
    getCurrentTime >>= print
    let inputInt = read inputStr    -- this line is pure
    let result = fib inputInt       -- this is also pure
    putStrLn "Result:"
    print result
    putStrLn "Ending time:"
    getCurrentTime >>= print

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

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

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

是的!你说对了!Now()或CurrentTime()或任何这种风格的方法签名都没有以某种方式显示引用透明性。但是通过对编译器的指令,它是由系统时钟输入参数化的。

通过输出,Now()可能看起来没有遵循引用透明性。但是系统时钟和它上面的功能的实际行为是坚持的 引用透明性。

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

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

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

您正在讨论函数式编程中一个非常重要的主题,即执行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 ()

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