99 questions/Solutions/18
From HaskellWiki
(Difference between revisions)
(fix the introduced error) |
(adding a remark; adding after hask-tagged words, as theres no trailing space after hask tagged word as rendered by IE6 (a bug? someone knows how to fix it?)) |
||
| Line 41: | Line 41: | ||
</haskell> | </haskell> | ||
| - | Another way using <hask>splitAt</hask>, though not nearly as elegant as the <hask>take</hask> and <hask>drop</hask> version: | + | Another way using <hask>splitAt</hask>, though not nearly as elegant as the <hask>take</hask> and <hask>drop</hask> version: |
<haskell> | <haskell> | ||
| Line 55: | Line 55: | ||
</haskell> | </haskell> | ||
| - | A solution using <hask>zip</hask>, <hask>filter</hask> then <hask>map</hask> seems straight-forward to me | + | A solution using <hask>zip</hask>, <hask>filter</hask> then <hask>map</hask> seems straight-forward to me (''NB: this won't work for infinite lists''): |
<haskell> | <haskell> | ||
Revision as of 06:14, 1 June 2011
(**) Extract a slice from a list.
Given two indices, i and k, the slice is the list containing the elements between the i'th and k'th element of the original list (both limits included). Start counting the elements with 1.
slice xs i k | i>0 = take (k-i+1) $ drop (i-1) xs
The same solution as above, but the more paranoid (maybe too paranoid?) version of it (uses guards and Maybe):
slice :: [a] -> Int -> Int -> Maybe [a] slice [] _ _ = Just [] slice xs k n | k == n = Just [] | k > n || k > length xs || n > length xs || k < 0 || n < 0 = Nothing | k == 0 = Just (take n xs) | otherwise = Just (drop (k-1) $ take n xs)
Or, an iterative solution:
slice :: [a]->Int->Int->[a] slice lst 1 m = slice' lst m [] where slice' :: [a]->Int->[a]->[a] slice' _ 0 acc = reverse acc slice' (x:xs) n acc = slice' xs (n - 1) (x:acc) slice (x:xs) n m = slice xs (n - 1) (m - 1)
Or:
slice :: [a] -> Int -> Int -> [a] slice (x:xs) i k | i > 1 = slice xs (i - 1) (k - 1) | k < 1 = [] | otherwise = x:slice xs (i - 1) (k - 1)
splitAt
take
drop
slice :: [a] -> Int -> Int -> [a] slice xs i k = chunk where chop = snd $ splitAt i' xs -- Get the piece starting at i chunk = fst $ splitAt (k - i') chop -- Remove the part after k i' = i - 1
splitAt
slice xs (i+1) k = snd (split (fst (split xs k)) i)
zip
filter
map
slice xs i j = map snd $ filter (\(x,_) -> x >= i && x <= j) $ zip [1..] xs
