I have a large nested vector that look like this:
import Data.Vector
let x = fromList [["a", "b", "12", "d"], ["e", "f", "34", "g"]...]
I would like to convert the strings to integers at position 2 in each nested list I was trying to do this with map and a comprehension like this:
let y = Data.Vector.map (\a -> read a :: Int) [i !! 2 | i <- x]
What am I doing wrong? I would like the output to be:
(("a", "b", 12, "d"), ("e", "f", 34, "g")...)
There are a number of problems here.
First of all, the result of a list comprehension is a list, so you're calling Data.Vector.map on a list, which won't work. And the x inside the comprehension is a Vector, which is another type mismatch. Either use a list instead of a Vector (along with Prelude.map) or convert the list to a Vector (in which case you can't use a list comprehension).
Secondly, ignoring the list/Vector problem, [i !! 2 | i <- x] will give you a list containing only the elements at position 2 from each sub-list. Using your example, the comprehension would yield ["12", "34"]. Then when you map read over it, you'll get [12, 34], rather than the output you're shooting for.
Finally, the output you're wanting to see is not valid for lists or for Vectors in Haskell. Both types of container must be homogeneous, i.e. they cannot contain values of more than one type. A [Int] cannot contain Strings, nor can a [String] contain Ints, but your desired output contains both. There are ways you can get around this using existential types, but chances are there's a better solution for your underlying problem than to try to build heterogeneous collections.
Edit: You edited the last part of your post to use tuples, so the above paragraph no longer applies. The first two problems I mentioned still exist, though.
If you start with a list of 4-tuples ([(String, String, String, String)]), you can get what you want like this:
> let x = [("a", "b", "12", "d"), ("e", "f", "34", "g")]
> map (\(a, b, c, d) -> (a, b, read c :: Int, d)) x
[("a", "b", 12, "d"), ("e", "f", 34, "g")]
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