Haskell prefix unary operator form

In GHCi:

  • Prelude> (+3) 2
    5
  • Prelude> (* 3) 2
    6
  • Prelude> (/ 3) 2
    0.666666666666666666
  • Prelude> (-3) 2
    There is no instance for (Num (t → t1))
     arising from the literal it ': it = (- 3) 23' at <interactive>:1:2
    Possible fix: add an instance declaration for (Num (t -> t1))
    In the expression: 3
    In the expression: (- 3) 2
    In the definition of

How can I fix the latter so that it returns -1?

+3
source share
4 answers

Haskell grammar does not allow you to use -. Use the function instead subtract:

(subtract 3) 2
+12
source

grddev, Haskell 98 Report:

-e , Haskell, negate (e). - - ; . - negate, . - .

, , : , - , :info (+) :info (-) .

subtract, grddev, infix:

Prelude> let (#) = (-)
Prelude> (# 3) 2
-1

subtract , , .

+7

(-) 3 2

1. -1, 3 -, ,

flip (-) 3 2
+1

, :

(+ -3)

, .

0

Source: https://habr.com/ru/post/1758017/


All Articles