gpt4 book ai didi

haskell - Haskell 中的文学编程真的是 "literate programming"吗?

转载 作者:行者123 更新时间:2023-12-03 14:55:38 26 4
gpt4 key购买 nike

我是识字编程概念的新手。我正在阅读 Donald Knuth's paper (PDF)关于这个主题,在一开始,在引言中,他说:

Instead of imagining that our main task is to instruct a computer what todo, let us concentrate rather on explaining to human beings what we wanta computer to do.

He or she [the practitioner of literate programming] strives for a programthat is comprehensible because its concepts have been introduced in anorder that is best for human understanding, using a mixture of formaland informal methods that reinforce each other.


然后,进一步阅读:

A thing about a program is its structural relationships.A complex piece of software consists of simple parts andsimple relations between those parts; the programmer’stask is to state those parts and those relationships,in whatever order is best for human comprehension—not in some rigidly determined order like top-down orbottom-up.

(...)

Top-down programming gives you a strong idea ofwhere you are going, but it forces you to keep a lot of plans in yourhead; suspense builds up because nothing is really nailed down untilthe end. programming has the advantage that you continually wield amore and more powerful pencil, as more and more subroutines have beenconstructed; but it forces you to postpone the overall programorganization until the last minute, so you might flounder aimlessly.

Thus the WEB language allows a person to expressprograms in a “stream of consciousness” order. TANGLEis able to scramble everything up into the arrangementthat a PASCAL compiler demands.This feature of WEB is perhaps its greatest asset;


上面的摘录让我对这个主题很感兴趣,所以我调查了一下。从任何搜索引擎提供的结果中不难看出 Haskell 和 literate 编程之间的关系,但我没有看到“人类理解的最佳顺序”。相反,我看到了一个非常完善的文档,同时保持了计算机工作所需的顺序。
  • 你能用“文学编程”这个词来解决这个顺序问题吗?
  • 有没有其他不需要“意识流”顺序特征的文学编程定义?
  • Haskell 真的能够读写编程吗(使用 Knuth 的定义)?

  • 最后,作为个人观点,我不得不说,即使 Haskell 所做的(可能还有许多其他语言所做的)也不是 Knuth 的文学编程,当涉及到对方法和算法的详尽描述时,我仍然喜欢这个想法。当评论远远超过代码时,它会起到很好的作用。

    † WEB 和 TANGLE 是最初 D. Knuth 在他第一次实现识字编程概念时使用的系统的一部分。

    最佳答案

    在大多数情况下,Haskell 在订购方面非常灵活。在 Haskell 模块中,所有的声明和定义可以以任何顺序出现。由于引用透明性,提取子问题以便在其他地方实现它们也很容易。

    顺序很重要的主要领域是模式匹配。函数的所有方程必须在一起,并且由于它们按顺序匹配,因此它们不能总是重新排序。但是,我不认为这是一个重大限制,因为 (a) 这通常是非常局部的,并且 (b) 具有大量方程的大函数可以(并且可能应该)重构为更小的部分,然后更容易重新排序.

    也许最烦人的约束是导入声明必须在模块的顶部。这确实稍微打破了意识流,许多用 Haskell 编写的识字程序都以“不要介意这些导入,稍后你会看到我们需要它们的地方”之类的声明开始。如果能够在文本中更自然的点命名导入,从上下文中可以清楚地知道为什么需要它们,这将更符合 Knuth 对文学编程的定义。

    除此之外,我认为 Haskell 非常适合文学编程。它可能不完全符合 Knuth 的定义,但我认为它已经足够接近了。

    关于haskell - Haskell 中的文学编程真的是 "literate programming"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16869050/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com