评估 Haskell 数据类型中未定义的元素

发布于 2024-11-17 04:28:52 字数 189 浏览 3 评论 0 原文

如果我尝试 > fst(a, b) 其中 ab 未定义,我收到 b 未定义的错误。即使在尝试 snd(a, b) 时,也是 b 首先导致错误。我有命令式编程的背景。我想知道这是否是某种我不理解的懒惰。

if I try > fst(a, b) where a, b are undefined, I get the error that b is undefined. Even on trying snd(a, b) it is b that causes the error first. I have a background in imperative programming. I am wondering if this is some kind of laziness that I don't understand.

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

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

发布评论

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

评论(3

反目相谮 2024-11-24 04:28:52

我认为 FUZxxl 的评论绝对正确。当我在 Hugs 的 repl 中输入:

Hugs> fst(a,b)
ERROR - Undefined variable "b"
Hugs> snd(a,b)
ERROR - Undefined variable "b"

这不是一个懒惰/热切的评估事情 - 当 Hugs 检查以确保 fst(a,b) 是有效的 Haskell 代码时,它注意到 ab 未定义。这两个字母在 Haskell 中没有特殊含义,它们就像在任何其他语言中一样是变量!

就像在 Java 中一样:

System.out.println(a);

永远不要说 a 是什么!您可以这样写:

String a = "Hello world."
System.out.println(a);

要解决此问题,您可以在 let 语句,例如:

>let (a,b) = (1,2) in fst(a,b) 

or

>let tup = (1,2) in fst tup

>let a=1;b=2 in fst(a,b)

or where 语句

>fst(a,b) where a=1;b=2

等。

或者,在某个名为无论什么的文件中定义(例如,“TestTuple.hs”)

a = 1
b = 2

并在 Hugs 中,转到:

>:load TestTuple.hs
>fst(a,b)
1

虽然您注意到您正在使用 Hugs,但只是为了参考,在 GHCi 中,您还可以在 REPL 中定义变量,例如 这:

>let a = 1
>let b = 2
>fst(a,b)
 1
>snd(a,b)
 2

I think FUZxxl's comment is absolutely correct. When I type into Hugs' repl:

Hugs> fst(a,b)
ERROR - Undefined variable "b"
Hugs> snd(a,b)
ERROR - Undefined variable "b"

This isn't a lazy/eager evaluation thing -- when Hugs checks to make sure that fst(a,b) is valid Haskell code, it notices that a and b aren't defined. Those two letters don't have special meanings in Haskell, they're variables like in any other language!

It's like in Java going:

System.out.println(a);

And never saying what a is! You'd instead write something like:

String a = "Hello world."
System.out.println(a);

To remedy this, you can either define a and b in a let statement, like:

>let (a,b) = (1,2) in fst(a,b) 

or

>let tup = (1,2) in fst tup

or

>let a=1;b=2 in fst(a,b)

or a where statement

>fst(a,b) where a=1;b=2

etc.

Alternatively, define in some file called whatever (for example, "TestTuple.hs")

a = 1
b = 2

and in Hugs, go:

>:load TestTuple.hs
>fst(a,b)
1

Although you note that you are using Hugs, just for reference, in GHCi, you can also define variables in the REPL like this:

>let a = 1
>let b = 2
>fst(a,b)
 1
>snd(a,b)
 2
归途 2024-11-24 04:28:52

您将看到以下内容:

Prelude> fst (undefined, undefined)
*** Exception: Prelude.undefined
Prelude> snd (undefined, undefined)
*** Exception: Prelude.undefined

如您所见,访问未定义的元素将得出未定义的值。

懒惰使我们能够避免评估整个结构,但是,

Prelude> snd (undefined, 2)
2

您的评论表明您可能忘记声明一些特定的变量,ab

Here's what you will see:

Prelude> fst (undefined, undefined)
*** Exception: Prelude.undefined
Prelude> snd (undefined, undefined)
*** Exception: Prelude.undefined

As you can see, accessing an undefined element evaluates to an undefined value.

Laziness allows us to avoid evaluating the entire structure, however,

Prelude> snd (undefined, 2)
2

Your comment suggests you might have forgotten to declare some specific variables, a and b.

花开柳相依 2024-11-24 04:28:52

我认为你的问题是为什么它抱怨 b 而不是 a,那是因为 haskell 任意评估参数。也就是说,您永远不知道首先评估哪一个。在你的情况下,显然,haskell 在 a 之前偶然评估了 b,这就是为什么它抱怨 b 而不是 a。

I think your question was why is it complaining about b but not a, and that is because haskell evaluates arguments arbitrarily. That is, you never know which one is evaluated first. In your case, apparently, haskell evaluated b before a by chance and that's why it complains about b but not a.

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