4 ms·
Your first Python example is just numeric coercion, not weak typing. Haskell does numeric coercion too (try 2 + 1.1 in ghci). C does not behave like Python and
by djur 12y ago
Your first Python example is just numeric coercion, not weak typing. Haskell does numeric coercion too (try 2 + 1.1 in ghci). C does not behave like Python and print 42 in this example. Try it yourself and see.
True being coercible to a number is unfortunate, but it's still not the same as what C does with that printf.
"foo" * 3 is just an example of a polymorphic operator. You could implement the same in any strongly typed language that allows polymorphic operators.
- deleted 12y ago[deleted]
- tikhonj 12y agoHaskell doesn't do numeric conversion, it just allows numeric literals to have any valid numeric type. If you tried it with variables you would get a type error: > let x = 1.0 :: Double > let y = 1 :: Int x + y <interactive>:10:5: Couldn't match expected type ‘Double’ with actual type ‘Int’ In the second argument of ‘(+)’, namely ‘y’ In the expression: x + y It sometimes looks like conversion because the literal `1` is polymorphic and can have a more specific type like `Int` or `Double` inferred from context.
- djur 12y agoAh, thanks for the correction.
- michaelochurch 12y ago(Different poster.) In fact, one of the beautiful things about Haskell is that the type system can (usually) infer exactly as much specificity as is needed. λ> :t 5 5 :: Num a => a λ> :t 5.6 5.6 :: Fractional a => a λ> :t (5 :: Integer) (5 :: Integer) :: Integer The type of the literal "Num a => a" means that the literal "5" works for any type in the Num typeclass. So you can mix literals with any numeric type because the compiler will correctly infer the right type. You can even get usages like this: λ> :t (\x -> x + 5) (\x -> x + 5) :: Num a => a -> a λ> let f = (\x -> x + 5) in (f (0 :: Int), f (0 :: Double)) (5,5.0)
- ForHackernews 12y ago> True being coercible to a number is unfortunate If we want to be really pedantic, in Python 2.X, True isn't coercible to an integer, it is an integer. >>> True == 1 True >>> isinstance(True, int) True This is an artifact of the fact that when Python was first created, it didn't have boolean types, so when they were added later, "True" and "False" were bolted on as aliases to the integers 1 and 0.