Get a sublist in Haskell?

First of all, that's not an array, it's a list. I'm not being (merely) pedantic, as arrays are much more problematic in Haskell than lists.

First of all, that's not an array, it's a list. I'm not being (merely) pedantic, as arrays are much more problematic in Haskell than lists. That said, one common way is to use take and drop together: Prelude> drop 4 .

Take 9 $ 1,2,3,4,5,6,7,8,9,0 5,6,7,8,9 Prelude> take (9-4) . Drop 4 $ 1,2,3,4,5,6,7,8,9,0 5,6,7,8,9 The latter is a bit more efficient.

Awesome thanks - I've also corrected it to say list rather than array :) – PeterM Dec 16 at 4:51 1 @ehird: You just listed the main reasons why I called them problematic. What I didn't call them was "not useful" and "harmful" :-) – ibid Dec 16 at 5:20 1 Generalizing: slice begin end = take (end - begin) . Drop begin - also I'm pretty sure GHC could optimize out the inefficiency of the first imlpementation.

– Dan Burton Dec 16 at 5:49 1 Yes, I'm pretty sure the efficiency comment is inaccurate; even with a simple non-strict evaluation you never look "too far" into the list, so unless GHC pessimises this, which I doubt, everything should be fine with the first, clearer version. – ehird Dec 16 at 6:07 2 @ehird, naively it would be less efficient because each value dropped would be computed from the take thunk (extra compare and increment). While the second version does all the dropping at once, so take operates with the plain list.

Obviously, profiling is essential if this is performance critical. Think about it this way: to get the first value, the first version needs to get 5 from take, while the second version only gets one element from take. Both versions do the same amount of work in drop and computing the list.

– Philip JF Dec 16 at 7:50.

You may be interested in Data. Vector (slice). Ghci> import Data.

Vector ghci> let v = fromList 1..10 ghci> v fromList 1,2,3,4,5,6,7,8,9,10 ghci> slice 4 5 v fromList 5,6,7,8,9 Note that slice in Data. Vector takes as inputs the beginning index and the length of the slice.

Drop 4 (take 9 1,2,3,4,5,6,7,8,9,0) 5,6,7,8,9.

How about map snd . Filter (liftA2 (&&) (>= 4) (Zip 0.. (note (Filter (f. Fst) .

Zip 0.. or a generalization in the libs? – Landei 2 days ago @Landei: Seems of too limited use to me. There are usually ways to solve things without resorting to zipping indices.

– ehird 2 days ago.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions