Haskell 中的 I/O 是函数式的吗? [英] I/O in Haskell is Functional?
问题描述
我刚刚开始看看 Haskell(我以前的 FP 经验是在 Scheme 中),我遇到此代码:
I'm just starting to take a look at Haskell (my previous FP experience is in Scheme), and I came across this code:
do { putStrLn "ABCDE" ; putStrLn "12345" }
对我来说,这是过程式编程,如果有的话——尤其是因为副作用的连续性.
To me, this is procedural programming, if anything -- especially because of the consecutive nature of side effects.
有人能解释一下这段代码在任何方面的功能"吗?
Would someone please explain how this code is "functional" in any respect?
推荐答案
虽然它看起来是一个过程程序,但上面的语法被翻译成一个函数式程序,如下所示:
While it appears to be a procedural program, the above syntax is translated into a functional program, like so:
do { putStrLn "ABCDE" ; putStrLn "12345" }
=>
IO ( s -> case (putStrLn "ABCDE" s) of
( new_s, _ ) -> case (putStrLn "12345" new_s) of
( new_new_s, _) -> ((), new_new_s))
也就是说,一系列嵌套函数有一个独特的世界参数贯穿它们,程序性地"对原始函数的调用进行排序.此设计支持将命令式编程编码为函数式语言.
That is, a series of nested functions that have a unique world parameter threaded through them, sequencing calls to primitive functions "procedurally". This design supports an encoding of imperative programming into a functional language.
对基于此设计的语义决策的最佳介绍是 "The Awkward Squad" 论文,
The best introduction to the semantic decisions underlying this design is "The Awkward Squad" paper,
这篇关于Haskell 中的 I/O 是函数式的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!