How this function works: const const (negate 1) (negate 2) 3

I am studying Haskell atm, now I found this function

const const (negate 1) (negate 2) 3

The result of this function is -2 . I do not understand why the result is not -2.

const :: a -> b -> a
negate :: Num a => a -> a

So I thought that I can also set the brackets as follows: const (const (negation 1) (negation 2)) 3 But now I get the result -1.

Where is my fault I do not understand how this works.

+4
source share
2 answers

Haskell parsing rules are very simple. If we ignore infix operators ( +, &&etc.), there is only one rule:

a b c d eparsed as (((a b) c) d) e.

, , a (b c d) e. ( , , .)

,

( ( (const const) (negate 1) ) (negate 2) ) 3

const const (negate 1) const. (negate 2) const 3.

+12

:

(((const const) (negate 1)) (negate 2)) 3

(const const) a -> b -> c -> b, , -2.

+4

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


All Articles