[Haskell-cafe] How to get subset of a list?

Gene A yumagene at gmail.com
Thu Dec 7 04:01:53 EST 2006


On 11/30/06, Huazhi (Hank) Gong <hankgong at nm.gist.ac.kr> wrote:
>
> Thanks, it make sense here.
> However, like I want to choose s[1,3,6,10] or something like this. Are there
> some straightforward function or operator for doing this job? The !!
> operator in haskell seems does not support multiple indecies.

hI,
  Hadn't checked my mail in a few days.... but here goes:

helper function:

index' :: forall a. [a] -> Int -> [a]
index' xs i = take 1 $ drop (i-1) xs

The real deal:

index :: forall a. [a] -> [Int] -> [[a]]
index cs xs = map (index' cs) xs

simple example of use:

Prelude> index "ABCDEFGHIJKLMNOPQRSTUVWXYZ" [2,5..26]
["B","E","H","K","N","Q","T","W","Z"]

happy computing,
gene


More information about the Haskell-Cafe mailing list