Haskell:非严格的布尔运算

栏目: 编程语言 · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/10951353/haskell-non-strict-boolean-operations
无论如何在 Haskell

中定义如下的函数?

or True      True      = True
or True      undefined = True
or True      False     = True
or undefined True      = True
or undefined False     = undefined
or undefined undefined = undefined
or False     True      = True
or False     undefined = undefined
or False     False     = False

我目前没有它的用​​例(虽然我对它感兴趣),我只是感兴趣,如果可能的话.

根据您的评估顺序(检查值的顺序),您可以编写一个惰性版本:

Prelude> True || undefined
True
Prelude> undefined || True
*** Exception: Prelude.undefined

Prelude> :t or
or :: [Bool] -> Bool

Prelude> or [True, undefined]
True

实际上,Haskell中的默认定义将表现得像这样,因为Haskell是一种惰性语言.

但是,没有办法“跳过”未定义的值,而不先查看值,这会将其评估为底部,这会导致表达式未定义.

请记住,懒惰的值会出现在其中的鬼魂中:

如果你看看盒子内部,鬼可能会得到你.

如果检查底部很重要(例如作为测试套件的一部分),您可以将它们视为异常, and intercept them .但是您不会在纯函数中执行此操作.

翻译自:https://stackoverflow.com/questions/10951353/haskell-non-strict-boolean-operations


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computing Patterns in Strings

Computing Patterns in Strings

Bill Smyth / Addison Wesley / 2003 / $ 75.00

The computation of patterns in strings is a fundamental requirement in many areas of science and information processing. The operation of a text editor, the lexical analysis of a computer program, the......一起来看看 《Computing Patterns in Strings》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具