简体   繁体   中英

How to apply function to vector list?

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] [i !! 2 | i <- x] [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, ie they cannot contain values of more than one type. A [Int] cannot contain String s, nor can a [String] contain Int s, 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")]

It looks much like you should use a more sophisticated data type than a 4-tuple, like

data YourType_StringNum = YourType_StringNum { ytsnLetters1 :: String
                                             , ytsnLetters2 :: String
                                             , ytsnNumber :: String
                                             , ytsnLetters3 :: String }
data YourType_IntNum = YourType_IntNum { ytinLetters1 :: String
                                       , ytinLetters2 :: String
                                       , ytinNumber :: Int
                                       , ytinLetters3 :: String }

(of course with better identifiers). Then define a function like

toYtin :: YourType_StringNum -> YourType_IntNum
toYtin(YourType_StringNum a b s c) = YourType_IntNum a b (read s) c

With that, your problem reduces to transforming a Vector YourType_StringNum to a Vector YourType_IntNum , and that's trivially done with Data.Vector.map toYtin .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM