Anas Nagaty
Anas Nagaty

Reputation: 151

Convert a list of strings to a list of integers

I have a list:

ls = ["1000", "2000", "4000", "8000"]

I want to convert the following list to a list of strings like the following:

ls = [1000, 2000, 4000, 8000]

I tried the following but it didn't work:

let val = (read ls :: Integer)

Upvotes: 2

Views: 71

Answers (2)

Will Ness
Will Ness

Reputation: 71065

To apply a function to each member of a list, the function map :: (a -> b) -> [a] -> [b] is used, which creates the list of results of said applications.

So then you need to write

> let val = (map read ls :: [Integer])

> val
[1000,2000,4000,8000]
it :: [Integer]

Upvotes: 3

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476557

That will not work since you here use read for a list of strings. You should work with map :: (a -> b) -> [a] -> [b] to apply a function to all elements of a list:

let val = map read ls :: [Integer]

Upvotes: 4

Related Questions