[Haskell-cafe] Removing alternate items from a list

aditya siram aditya.siram at gmail.com
Tue Jun 8 10:03:19 EDT 2010


And for a few more lines of codes, you get a more flexible solution:
data Consume = Take | Skip

consumeBy :: [Consume] -> [a] -> [a]
consumeBy []                   _      = []
consumeBy _                    []     = []
consumeBy (tOrS:takesAndSkips) (x:xs) = case tOrS of
                                          Take -> x : consumeBy takesAndSkips xs
                                          Skip -> consumeBy
takesAndSkips xs

*Main> consumeBy (cycle [Take,Take,Skip]) [1,2,3,4,5,6]
[1,2,4,5]
*Main> consumeBy (cycle [Take,Take,Take,Skip]) [1,2,3,4,5,6]
[1,2,3,5,6]

-deech

On 6/8/10, Christopher Done <chrisdone at googlemail.com> wrote:
> Can't forget fix in a game of code golf!
>
>> (fix $ \f (x:_: xs) -> x : f xs) [1..]
> => [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,4...
>
> 2010/6/8 Yitzchak Gale <gale at sefer.org>:
>> R J wrote:
>>> What's the cleanest definition for a function f :: [a] -> [a] that takes
>>> a
>>> list and returns the same list, with alternate items removed?  e.g., f
>>> [0,
>>> 1, 2, 3, 4, 5] = [1,3,5]?
>>
>> f = map head . takeWhile (not . null) . iterate (drop 2) . drop 1
>>
>> Regards,
>> Yitz
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list