What is the equivalent to (+1) for the subtraction, since (-1) is seen as a negative number? [duplicate]

感情迁移 提交于 2019-12-01 15:38:49

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.

You can use the subtract function (which is in the Standard Prelude).

moveDn y = modifyMVar_ y $ return . (subtract 1)

You can also use flip to reorder the parameters that - takes.

moveDn y = modifyMVar_ y $ return . (flip (-) 1)

If the above-mentioned subtract is too verbose, you could try something like (+ (-1)) or (-1 +).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!