由于(-1)被视为负数,等于(+1)的相减是多少? [英] What is the equivalent to (+1) for the subtraction, since (-1) is seen as a negative number?

查看:170
本文介绍了由于(-1)被视为负数,等于(+1)的相减是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

柯鲁里减法


我开始了我的第一个不是来自教程的haskell项目,并且



我有以下代码:

  moveUp y = modifyMVar_ y $ return。 (+1)
moveDn y = modifyMVar_ y $ return。 (-1)

我花了一些时间来理解为什么我的代码不能编译:使用(-1)被视为负数。括号中的减号不会帮助它,因为它将它作为前缀并使其成为第一个参数。



简而言之,这是什么免费版?

  dec :: Num a => a  - > a 
dec x = x - 1


解决方案

我相信你需要方便地命名的减去函数,这正是您发现的原因:


  subtract :: Num a => a  - > a  - > a 

相同flip( - )

因为在Haskell语法中特别处理 - ,所以( - e)不是节,而是前缀否定的应用。但是,(减去exp)等同于不允许的部分。


如果你可以使用 flip( - )作为 Prelude 文档提及。但那是...有点丑。


Possible Duplicate:
Currying subtraction

I started my first haskell project that is not from a tutorial, and of course I stumble on the simplest things.

I have the following code:

moveUp y = modifyMVar_ y $ return . (+1)
moveDn y = modifyMVar_ y $ return . (-1)

It took me some time to understand why my code wouldn't compile: I had used (-1) which is seen as negative one. Bracketting the minus doesn't help as it prefixes it and makes 1 its first parameter.

In short, what is the point free version of this?

dec :: Num a => a -> a
dec x = x - 1

解决方案

I believe you want the conveniently-named subtract function, which exists for exactly the reason you've discovered:

subtract :: Num a => a -> a -> a

the same as flip (-).

Because - is treated specially in the Haskell grammar, (- e) is not a section, but an application of prefix negation. However, (subtract exp) is equivalent to the disallowed section.

If you wanted to write it pointfree without using a function like subtract, you could use flip (-), as the Prelude documentation mentions. But that's... kinda ugly.

这篇关于由于(-1)被视为负数,等于(+1)的相减是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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