[Haskell-cafe] How to take a minimum sub list that only contain certain number of elements of certain type?

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Tue Sep 25 23:24:32 CEST 2012


On 26 September 2012 03:56, Gwern Branwen <gwern0 at gmail.com> wrote:
> On Tue, Sep 25, 2012 at 1:42 PM, Rishabh Jain <rishabh11 at live.com> wrote:
>> f x 0 = []
>> f (x:xs) y | x `mod` 2 == 0 = x : (f xs y) | otherwise = x : (f xs (y-1))
>>
>>> f [0..] 4
>>> [0,1,2,3,4,5,6,7]
>
> Tsk, tsk. So ugly. How's this:
>
>> let f x = take x . filter odd
>>
>> f 4 [0..]
> ~> [1, 3, 5, 7]
>
> Notice that 0 is excluded, since 0 is *even*, not odd:
> http://en.wikipedia.org/wiki/Parity_of_zero
> If this is a serious problem, one can always just prepend zero:
>
>> 0 : f 4 [1..]
> ~> [0, 1, 3, 5, 7]

Except that your solution is incorrect, as Magicloud wanted the
smallest _prefix_ that contained four odd numbers...

>
> --
> gwern
> http://www.gwern.net
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe



-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
http://IvanMiljenovic.wordpress.com



More information about the Haskell-Cafe mailing list