简体   繁体   English

“没有操作”已经开始了

[英]“No operation” haskell

If I remember correctly from school, there's a function or keyword that is used for "not yet implemented" but the code compiles. 如果我从学校正确记得,有一个函数或关键字用于“尚未实现”,但代码编译。 I've tried to search for it, but can't find. 我试图搜索它,但找不到。 Any one know what I'm looking for? 谁知道我在找什么?

is's something like 就是这样的

isDivisor :: Integer -> Integer -> Bool
isDivisor x y = None
--isDivisor x y = (rem x y) == 0

What you're thinking of is called bottom 你想到的是bottom

bottom isn't just for showing something isn't implemented, it's meant to represent a computation which causes our program to fail. bottom不仅仅是为了显示没有实现的东西,它意味着代表一个导致我们的程序失败的计算。

For example we can actually define undefined ourselves as an infinite loop 例如,我们实际上可以将undefined自己定义为无限循环

undefined = let x = x in x
undefined = undefined

So really what we're doing is just putting in a value undefined :: a which will cause or program to crash or loop forever, but never evaluating it. 所以我们真正做的就是输入一个undefined :: a的值undefined :: a会导致或编程崩溃或永远循环的值,但从不评估它。

Therefore if you have some big and complex function that you don't know how to implement you could just do this 因此,如果你有一些大而复杂的功能,你不知道如何实现,你可以这样做

foo :: Bar -> Baz -> Quux
foo bar baz = foo bar baz

Since this typechecks, it'll compile and we can test other parts of our program. 从这个类型检查,它将编译,我们可以测试我们的程序的其他部分。

However since it's pretty unhelpful to have an infinite loop when you accidentally run that part of the program, GHC and others implement undefined as a differently. 但是,因为当你意外地运行程序的那一部分时,无限循环是非常无益的,所以GHC和其他人以不同方式实现undefined They have them crash the program and emit an error message, eg: 他们让程序崩溃并发出错误消息,例如:

-- In GHC
error msg = throw (ErrorCall s)
undefined = error "Prelude.undefined"

So to leave a function undefined with better debugging capabilities 因此,保留一个未定义的函数,具有更好的调试功能

foo bar baz = undefined
foo bar baz = error ("Tried to evaluate foo with" ++ show bar ++ show baz)

If you're finding the concept of bottom confusing, hammar posted a great answer 如果你发现底层混乱的概念,哈马尔发布了一个很好的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM