2 ms·
> In Python you have a simple function, `int()` that will happily turn any string consisting of numbers into a proper integer that can be used for math operatio
by valcron1000 3y ago
> In Python you have a simple function, `int()` that will happily turn any string consisting of numbers into a proper integer that can be used for math operations.
For starters this is not the case, it does not work on a string of numbers:
>>> int("123 890 123")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '123 890 123'
> Before we can select a function to convert a string into a number we must first pick a numerical type and before we can do that we must think real hard about how big of a number we're going to support in this application
Aren't you doing the exact same thing in Python by picking `int()` over `float()`?
> what type of string are we going to get from the command line?
Well, this might be the case for system languages, but most have a single `String` type.
Here is the program you described in Python
def main():
n1 = input()
n2 = input()
sum = int(n1) + int(n2)
print(sum)
And here it's in Haskell
main = do
n1 <- getLine
n2 <- getLine
let sum = read n1 + read n2
print sum
Accepting a single line of numbers is not much different
main = do
nums <- words <$> getLine
let res = sum $ map read nums
print res
I didn't think much about it, it's not a lot of code and I guess it's easy to reason about?