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

Rishabh Jain rishabh11 at live.com
Tue Sep 25 19:42:26 CEST 2012


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]
> To: haskell-cafe at haskell.org
> From: jon.fairbairn at cl.cam.ac.uk
> Date: Tue, 25 Sep 2012 10:16:52 +0100
> Subject: Re: [Haskell-cafe] How to take a minimum sub list that only contain	certain number of elements of certain type?
> 
> Magicloud Magiclouds <magicloud.magiclouds at gmail.com> writes:
> 
> > Hi,
> >   For example, I have an array [0..]. Now I want to take a sub list
> > that starts from index 0, and only contain 4 odds, and is minimum
> > result. The answer should be [0, 1, 2, 3, 4, 5, 6, 7].
> >   How to do that? Combining lazy computing, I cannot figure out an
> > efficient algorithm.
> 
> Does
> 
> f bound odds_so_far [] = []
> f bound odds_so_far (x:xs)
>  | odds_so_far == bound = []
>  | otherwise = x : f bound (odds_so_far + if odd x then 1 else 0) xs
> 
> required_funciton = f 4 0
> 
> meet your criteria, or am I missing something?
> 
> -- 
> Jón Fairbairn                                 Jon.Fairbairn at cl.cam.ac.uk
> 
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120925/c4955a4a/attachment.htm>


More information about the Haskell-Cafe mailing list