Why is it impossible to multiply negative numbers in haskell without brackets

Multiplication 5 * -3in haskell gchi gives me an error as well. But multiplication 5 * (-3)works fine. Why are parentheses needed?

$ ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> 5 * -3

<interactive>:2:1:
    Precedence parsing error
        cannot mix `*' [infixl 7] and prefix `-' [infixl 6] in the same infix expression
Prelude> 5 * (-3)
-15
Prelude>
+4
source share
6 answers

Due to the way the unary minus operator is specified in Haskell. Unfortunately, this is a bit of magic. This is a strange corner of the language specification.

Unary minus is the only unary operator in the language. Unary syntax support minus is just syntax sugar support for a function negate.

, - 7 , -7. , , .

, , .

, :

5 * (-3)
+8

, - ( Haskell) .

Haskell, , . , 5*-3.

5 * -3 5 *- 3?

+3

- , , .

: .

, , :

haskell .

:

Precedence parsing error
    cannot mix `*' [infixl 7] and prefix `-' [infixl 6] in the same infix expression

? , , infixl 7 infixl 6. - :

infixr 9 .
infixr 8 ^, ^^, **
infixl 7 *, /, `quot`, `rem`, `div`, `mod`
infixl 6 +, -
infixr 5 :
infix 4 ==, /=, <, <=, >=, >
infixr 3 &&
infixr 2 ||
infixl 1 >>, >>=
infixr 1 =<<
infixr 0 $, $!, `seq`

. , , .

: infixl 7 infixl 6, , - :

Prelude> 5 * +3

:

parse error on input `+'

, sintax, ?

, , unary "-" operator haskell ( ) , - .

unary "-" operator, .

+2

, - - Haskell . , , , 5 * -3 , , .

, : .

, , C- (, Haskell), , , -3 . , *.

0

, , - . .

0

Because -3 is not a negative number, as in other languages. This is a unary function followed by Num 3.

-1
source

All Articles