why cant an Int and a floating point number be added in haskell -


why wont work :-

(length [1,2,3,4]) + 3.2 

while works:-

2+3.3 

i understand in first case result int+float not same in second case too, or haskell automatically infer type in second case :- num+num whereas not in first case?

haskell never implicit type conversion you. + ever works on 2 numbers of same type, , gives type result well. other usage of + error, saw (length [1,2,3,4]) + 3.2 example.

however, numeric literals overloaded in haskell. 2 numeric type, , 3.3 fractional type. when haskell sees expression 2 + 3.3 can try find type both "numeric" , "fractional", , treat both numbers type addition work.

speaking more precisely, + has type num => -> -> a. 2 on own of type num => a , 3.3 on own of type fractional => a. putting 3 types together, in expression 2 + 3.3 both numbers can given type fractional => a, because fractional types num types, , satisfies type of +. (if type expression ghci a gets filled in double, because ghc has default type something in order evaluate it)

in expression (length [1,2,3,4]) + 3.2, 3.2 still overloaded (and in isolation have type fractional => a). length [1,2,3,4] has type int. since 1 side fixed concrete type, way satisfy type + fill in a on other type int, violates fractional constraint; there's no way 3.2 int. expression not well-typed.

however, integral type (of int one) can converted any num type applying fromintegral (this how integer literals 2 can treated numeric type). (fromintegral $ length [1,2,3,4]) + 3.2 work.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -