How to get subset of a list?

Like given a string list s="This is the string I want to test", I want to get the substring. In ruby or other language, it's simple like s[2..10], but how to do it in Haskell? -- View this message in context: http://www.nabble.com/How-to-get-subset-of-a-list--tf2735647.html#a7631994 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Thu, Nov 30, 2006 at 05:47:43PM -0800, Huazhi (Hank) Gong wrote:
Like given a string list s="This is the string I want to test", I want to get the substring. In ruby or other language, it's simple like s[2..10], but how to do it in Haskell?
Use take and drop, from the Prelude: (ghci session) Prelude> "Hello world" "Hello world" Prelude> drop 3 "Hello world" "lo world" Prelude> take 7 (drop 3 "Hello world") "lo worl" Prelude>

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. Hank Stefan O wrote:
On Thu, Nov 30, 2006 at 05:47:43PM -0800, Huazhi (Hank) Gong wrote:
Like given a string list s="This is the string I want to test", I want to get the substring. In ruby or other language, it's simple like s[2..10], but how to do it in Haskell?
Use take and drop, from the Prelude:
(ghci session) Prelude> "Hello world" "Hello world" Prelude> drop 3 "Hello world" "lo world" Prelude> take 7 (drop 3 "Hello world") "lo worl" Prelude> _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-to-get-subset-of-a-list--tf2735647.html#a7632145 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On 11/30/06, Huazhi (Hank) Gong
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.
If you're trying to do random access on a list, you should rethink why
you're using a list.
--
Taral

Your curious example suggests you might be solving a more specialized problem, like selecting the diagonal of a flattened matrix. In this case, there are much better (and more efficient) data structures that enforce invariants (like squareness of a matrix), if that is what you in fact are doing. Taral wrote:
On 11/30/06, Huazhi (Hank) Gong
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.
If you're trying to do random access on a list, you should rethink why you're using a list.

Hello Huazhi, Friday, December 1, 2006, 5:04:10 AM, you wrote:
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.
just change your mind, Neo ;) Hugs> map ("abcdefg" !!) [1,3,6] "bdg" :: [Char] -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Good. I personally perfer this solution... Bulat Ziganshin-2 wrote:
Hello Huazhi,
Friday, December 1, 2006, 5:04:10 AM, you wrote:
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.
just change your mind, Neo ;)
Hugs> map ("abcdefg" !!) [1,3,6] "bdg" :: [Char]
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- View this message in context: http://www.nabble.com/How-to-get-subset-of-a-list--tf2735647.html#a7637491 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On 11/30/06, Huazhi (Hank) Gong
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

On 01/12/2006, at 12:47 PM, Huazhi (Hank) Gong wrote:
Like given a string list s="This is the string I want to test", I want to get the substring. In ruby or other language, it's simple like s [2..10], but how to do it in Haskell?
If your indices are in ascending order, and unique, then something like this might do the trick: els1 indexes list = els' (zip [0..] list) indexes where els' [] _ = [] els' _ [] = [] els' ((j,x):xs) indexes@(i:is) | i == j = x : els' xs is | otherwise = els' xs indexes Of course this is a right fold, so you ought to be able to use foldr. Here's an attempt: els2 indexes list = foldr comb undefined [0..] list indexes where comb _ _ [] _ = [] comb _ _ _ [] = [] comb j rec (x:xs) indexes@(i:is) | j == i = x : rec xs is | otherwise = rec xs indexes Bonus marks for figuring out why I used "undefined". Warning: this is largely untested code. Cheers, Bernie.
participants (7)
-
Bernie Pope
-
Bulat Ziganshin
-
Dan Weston
-
Gene A
-
Huazhi (Hank) Gong
-
Stefan O'Rear
-
Taral