I've got a list of strings, is it possible to convert it to an list of ints?
E.g.:
["1","2"] -> [1,2]
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList); Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object .
Method 1: Using eval() Python eval() function parse the expression argument and evaluate it as a python expression and runs Python expression(code), If the expression is an int representation, Python converts the argument to an integer.
You can also convert a string to a list using a separator with the split() method. The separator can be any character you specify. The string will separate based on the separator you provide. For example, you can use a comma, , , as the separator.
Well
f :: [String] -> [Int] f = map read
No?
This fails:
map read ["1","2"] [*Exception: Prelude.read: no parse
The way to do it is :
map (read::String->Int) ["1","2"] [1,2] :: [Int]
Outside of GHCI, in a .hs file it would be:
let intList = map (read::String->Int) ["1","2"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With