Haskell函数定义中“底”(⊥)的作用是什么? [英] What is the role of 'bottom' (⊥) in Haskell function definitions?

查看:84
本文介绍了Haskell函数定义中“底”(⊥)的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白 底部 _ | _ )在Haskell函数定义中。



的定义因为

 <$ c $  zip  例如描述为right lazy c> zip [] _ | _ = [] 

但我不清楚这与

  zip [] _ = [] 

在函数定义(例如上面的函数定义)中扮演 _ | _ 的角色是什么?特别是,与使用 _






更新和注意:作为优秀答案的读者会发现自己,这些答案的重要组成部分,值得在这里提出,是 不会(<一个href =https://stackoverflow.com/a/32513968/656912>并且不能),实际上,它出现在Haskell函数定义中请继续阅读

解决方案

<底部本质上就是说 undefined 的一种奇特的代数方式。如果你尝试这个,你可以看到为什么 zip 对于它的右边参数是懒惰的:

 λ> zip [] undefined 
[]
λ> zip undefined []
***例外:Prelude.undefined

这是因为 undefined 只会在您尝试评估它时失败。



您可能会混淆 _ | _ _ ,因为它被呈现的方式。我会说清楚:行 zip [] _ | _ = [] does not 充当模式匹配,但是是一个等式,说明等于 zip [] _ | _ [] 。也就是说,这不是有效的Haskell代码,而是一种符号化的抽象代数方式,表示我不关心第二个参数。



zip 的定义中,您当然可以使用 _ ,但这无关紧要。你可以使用任何名字,只要它不是一个构造函数匹配模式,比如(Just x)(a,b )。值必须保持未评估状态,直到它们必须在纯代码中进行模式匹配。



您可以阅读更多关于懒惰评估的信息 here

你可以阅读更多关于底部这里这里


I don't understand the role played by bottom ( or _|_) in Haskell function definitions.

The definition of zip for example describes it as "right lazy" because

zip [] _|_ = []

but I'm unclear how this differs from

zip [] _ = []

What role is _|_ playing in function definitions such as the one above? In particular, how is it different from using _?


UPDATE AND NOTE: As readers of the excellent answers will discover for themselves, a crucial part of those answers, worth pulling up here, is that does not (and cannot), in fact, appear in Haskell function definitions. Read on.

解决方案

Bottom is essentially the fancy algebraic way of saying undefined.

If you try this, you can see why zip is lazy for its right-hand argument:

λ> zip [] undefined
[]
λ> zip undefined []
*** Exception: Prelude.undefined

This is because undefined only fails when you try to evaluate it.

You might be confusing _|_ with _ because of the way it was presented. I will make it clear: the line zip [] _|_ = [] does not act as a pattern match but an equation, stating the equality of zip [] _|_ and []. That is to say, this is not valid Haskell code, but a notational, abstract-algebraic way of saying "I don't care about the second argument."

In the definition of zip you may of course use _, but that's irrelevant. You could have used any name, just as long as it wasn't a constructor-matching pattern such as (Just x) or (a,b). Values will remain unevaluated until they must be pattern matched in pure code.

You can read more about lazy evaluation here.

You can read more about bottom here and here.

这篇关于Haskell函数定义中“底”(⊥)的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆