你会写seq x x吗? [英] Would you ever write seq x x?

查看:128
本文介绍了你会写seq x x吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我并不完全清楚seq在Haskell中的工作方式。



好像有很多情况下写入

  seq xx 

,甚至可能定义一个函数:

  strict x = seq xx 

但是这样的函数还不存在,所以我猜这种方法有点不对劲。有人可以告诉我这是否有意义或有用吗? >返回 b 的值,但是该值取决于对 a 的评估。因此, seq aa a 完全一样。



我认为这里的误解是 seq 没有采取任何行动,因为纯函数不采取行动,它只是引入了依赖。



有一个函数 evaluate :: a - > IO() Control.Exception 中,它可以做你想做的事情(注意它在 IO )。他们把它放在异常中,因为看看表达式的评估是否会抛出是有用的,如果这样的话处理异常。


I'm not entirely clear on how seq works in Haskell.

It seems like it there are lots of cases where it would be useful to write

seq x x

and maybe even define a function:

strict x = seq x x

but such a function doesn't already exist so I'm guessing this approach is somehow wrongheaded. Could someone tell me if this is meaningful or useful?

解决方案

seq a b returns the value of b, but makes that value depend on the evaluation of a. Thus, seq a a is exactly the same thing as a.

I think the misunderstanding here is that seq doesn't take any action, because pure functions don't take actions, it just introduces a dependency.

There is a function evaluate :: a -> IO () in Control.Exception that does what you want (note that it's in IO). They put it in exception because it's useful to see if the evaluation of an expression would throw, and if so handle the exception.

这篇关于你会写seq x x吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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