Is there a difference between 2x and 2 * x?

Prelude> :t fmap (\x  -> 2x) []
fmap (\x  -> 2x) [] :: Num (t -> b) => [b]

Prelude> :t fmap (\x -> 2 * x) []
fmap (\x -> 2 * x) [] :: Num b => [b]

Prelude> :t 1 1
1 1 :: (Num (t -> t1), Num t) => t1

Prelude> :t 1 * 1
1 * 1 :: Num a => a

Is there any difference between 2xand 2*x?

What is the point Num (t -> t1) => t1?

+4
source share
1 answer

Is there a difference between 2x and 2 * x?

Yes. 2 * xis the number 2times the variable x. 2 xis the result of applying a function created by interpreting the literal value 2( fromInteger 2) of a variable x.

2 ? , Num (t -> t1), 2 :: t -> t1, , , . , Num. , , ​​ , - .

EDIT:

, :

{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts  #-}

instance (Num a) => Num (a -> a) where
  fromInteger n = \x -> ((fromInteger n) * x)

:

*Main> (\x -> (2 :: Int -> Int) x) 5
10

. IIRC, .. , 2 seconds * 5 hours.

+6

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


All Articles