Haskell 程序的基本结构

发布于 2025-01-02 07:48:10 字数 598 浏览 0 评论 0原文

我浏览过的许多 Haskell 教程几乎完全集中在语法上,很少涉及如何构建程序。

例如...

以下是 C++ 应用程序的简要概述:

#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  return 0;
}

当我第一次开始学习 C++ 时,此类示例极大地帮助我学习如何将各个部分组装成工作程序。也许我找错了地方,但我还没有找到任何对 Haskell 来说如此直接和简单的例子。

我已经了解很多 Haskell 语法。我可以编写递归列表推导式,操作字符串、整数,并列出 wazoo。

简而言之:我只想知道 Haskell 中的两个子例程和变量传递是什么样的。如果我能对如何构建 Haskell 程序有一些基本的了解,我最终可能能够将我学到的所有语法派上用场。

Many of the Haskell tutorials I've looked through focus almost entirely on syntax with very little coverage on how to structure a program.

For example...

Here's a bare-bones outline of a C++ application:

#include <iostream>
using namespace std;

int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  return 0;
}

When I first started learning C++, examples like these helped me immensely in learning how to assemble individual pieces into working programs. Maybe I'm looking in the wrong places, but I haven't been able to find any such examples that are as direct and simple for Haskell.

I already know A LOT of Haskell syntax. I can write recursive list comprehensions, and manipulate strings, integers, and lists out the wazoo.

In short: I just want to know what two subroutines and variable pass looks like in Haskell. If I can get some basic understanding on how to structure a Haskell program, I might finally be able to put all the syntax I've learned to some use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↘人皮目录ツ 2025-01-09 07:48:10

Haskell 程序的结构出奇的简单。您有一个执行 IO 操作的 main 函数,仅此而已。基础知识:

module Main where

addition a b = a + b

main :: IO ()
main = do let z = addition 5 3
          putStrLn $ "The result is: " ++ show z

现在您可以使用类似以下内容将其编译为一个简单的程序:

ghc --make Main.hs -o program

并且它应该生成一个名为program的可执行文件。

一般来说,程序的结构主要是进行实际计算的纯函数以及处理 IO 的代码的离散部分。 (当然也有例外,但是编写尽可能多的纯代码的总体思想是相当普遍的。)

由于几乎所有内容都表示为一堆纯函数,因此您不会在它们之间传递变量 - 函数进行通信通过传递参数。

执行 IO 的代码部分固定在 main 中。在大多数情况下,所有 IO 都会通过 main;即使你单独编写IO操作并给它们命名,它们最终也会从main开始执行。

Haskell 中的“函数组”称为“模块”。您可以有多个模块,每个模块都在自己的文件中:

module Blarg (exportedFunction) where

helper a b = ... -- this will *not* be exported
exportedFunction a b = helper a b -- this *will* be exported

只有括号中的标识符才会真正被导出;其余的都是隐藏的。如果您根本不包含括号,则默认情况下将导出所有内容。

将此文件另存为 Blarg.hs。现在您可以在 Main 中导入它:

import Blarg

对函数进行分组的另一种有用方法是 where

complicatedFunction a b c = helper 0
  where helper count = ...

这样 helper 仅在 complicatedFunction 的范围内 并且还可以从 complicatedFunction 访问 abc

A Haskell program's structure is surprisingly simple. You have a main function which does IO, and that's about it. So the basics:

module Main where

addition a b = a + b

main :: IO ()
main = do let z = addition 5 3
          putStrLn $ "The result is: " ++ show z

Now you can compile this into a simple program using something like:

ghc --make Main.hs -o program

and it should produce an executable called program.

In general, programs are structured as mostly pure functions doing the actual computation coupled with discrete parts of the code dealing with the IO. (There are of course exceptions to this, but the general idea of writing as much pure code as possible is fairly universal.)

Since almost everything is expressed as a bunch of pure functions, you do not pass variables between them--the functions communicate by passing arguments.

The part of your code that does IO is anchored in main. In most cases, all your IO is going to go through main; even if you write IO actions separately and give them names, they will ultimately be executed starting from main.

A "function group" in Haskell is called a "module". You can have multiple modules, each in its own file:

module Blarg (exportedFunction) where

helper a b = ... -- this will *not* be exported
exportedFunction a b = helper a b -- this *will* be exported

Only the identifiers in the parentheses will actually be exported; the rest are hidden. If you do not include the parentheses at all, everything will be exported by default.

Save this file as Blarg.hs. Now you can import it in Main:

import Blarg

Another useful way to group functions is where:

complicatedFunction a b c = helper 0
  where helper count = ...

This way helper is only in scope for complicatedFunction and also has access to a, b and c from complicatedFunction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文