本文介绍了Haskell中的I/O功能正常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚刚开始看一下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 best introduction to the semantic decisions underlying this design is "The Awkward Squad" paper,

这篇关于Haskell中的I/O功能正常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:07