Haskell: Index Vector Filtering Using Only Higher Order Basic Functions

Problem

I have a vector a of size N containing sample data, and another vector b of size M (N> M) containing indices. I would like to get a vector c of size N containing filtered elements from a based on the indices in b .

Question

Is it possible to implement the desired function without using list comprehensions, just basic functions of a higher order, such as map, zipWith, filter, etc. (more precisely, their equivalents are mapV, zipWithV, filterV, etc.)

Prerequisites:

I use the Haskell Embedded Domain Specific Language ( ForSyDe , ForSyDe.Shallow.Vector module ), limited by a set of hardware synthesized functions. To respect the design methodology, I am allowed to use only the provided functions (thus, I cannot use list methods, etc.).

+4
source share
2 answers

Denial of responsibility:

I did not test this code for functionality because cabal started listening. It worked well for lists, and when I converted each vector to a list, it should work fine, although problems may occur.


Try the following:

indexFilter :: (Num b, Eq b, Enum b) => Vector a -> Vector b -> Vector a
indexFilter vector indices = vector (map fst (filter (\x -> (snd x) `elem` (fromVector indices)) vectorMap))
 where
   vectorMap = zip (fromVector vector) [0..]

indexFilter (<element>, <index>), , b. vectorMap zip a .

+4

ThreeFx , - , (ForSyDe), :

  • ( ). ForSyDe : ( ) ( ). .
  • elem ForSyDe.Shallow.Vector

1

, , :

indexFilter1     :: (Num b, Eq b, Enum b) => Vector a 
                     -> Vector b 
                     -> Vector (Vector a)
indexFilter1 v = mapV (\idx -> selectV idx 1 1 v)

.

2

ThreeFx :

 indexFilter       :: (Num b, Eq b, Enum b) => Vector a 
                      -> Vector b 
                      -> Vector a
 indexFilter v idx = mapV (fst) (filterV (\x -> elemV (snd x) idx) vectorMap)
     where
       vectorMap = zipWithV (\a b -> (b, a)) (iterateV size (+1) 0) v
       size      = lengthV v
       elemV a   = foldlV (\acc x -> if x == a then True else acc) False
+4

All Articles