[Haskell-beginners] How is takeWhile implemented?

illusionoflife ghostlymailing at gmail.com
Mon Jul 9 19:18:54 CEST 2012


On Monday, July 09, 2012 18:01:49 Carlos J. G. Duarte wrote:
> underK k (x:xs) = (if x < k then [x] else []) ++ underK k xs
> 
> -- from [2] ... how does this work anyway?
> fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
> Which seems to work ok. How is takeWhile stopping at a given point and my
> underK not?
Look. In your implementation `underK' you *always* call itself. 
So, actually you implemented filter. takeWhile can be implemented(you can 
easily see source 
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html. 
Take notion that takeWhile do not call itself anymore if predicate is false.

> Other question is regarding the "$" and ".". I know the $ has lower priority
> and . has more, but is there any more difference that matters, I mean, can
> I always use the "$" everywhere. For example, this main also works: main =
> putStrLn $ show $ sum $ filter even $ takeWhile (<_M) fibs
> Is it doing the same thing?
It only affect amount of parensisis. Point is nice for high-order function 
magic.
-- 
Best regards, illusionoflife. 
This mail is for mailing lists.
For private, responce-warrantied 
mail use illusion.of.life92 at gmail.com.

	
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120709/dea3ffd8/attachment.pgp>


More information about the Beginners mailing list