Difference between revisions of "Talk:99 questions/Solutions/18"

From HaskellWiki
Jump to navigation Jump to search
(New page: Hi. I'm wondering if this solution still works in the current Haskell version. <haskell> slice xs (i+1) k = snd (split (fst (split xs k)) i) </haskell>)
 
m
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
 
slice xs (i+1) k = snd (split (fst (split xs k)) i)
 
slice xs (i+1) k = snd (split (fst (split xs k)) i)
 
</haskell>
 
</haskell>
  +
  +
  +
  +
The solution:
  +
<haskell>slice xs i k | i>0 = take (k-i+1) $ drop (i-1) xs</haskell>
  +
will not give proper results for negative indices particularly when the first index is negative and the second one within the length of the list.

Latest revision as of 08:29, 30 May 2012

Hi. I'm wondering if this solution still works in the current Haskell version.

slice xs (i+1) k = snd (split (fst (split xs k)) i)


The solution:

slice xs i k | i>0 = take (k-i+1) $ drop (i-1) xs

will not give proper results for negative indices particularly when the first index is negative and the second one within the length of the list.