[Haskell-cafe] filterFirst

Alexteslin alexteslin at yahoo.co.uk
Wed Jul 25 14:06:13 EDT 2007


Thank you Dan


Dan Weston wrote:
> 
> If you find it tedious to pass parameters that never change, remember 
> that you can access symbols defined in the enclosing environment 
> (closure), freeing you from "passing it on" each time:
> 
> filterAlpha :: (a -> Bool) -> [a] -> [a]
> filterAlpha f = filterAlpha'
>    where filterAlpha' [] = []
>          filterAlpha' (x:xs)
>            | f x         = x : (filterAlpha' xs)
>            | otherwise   =     (filterAlpha' xs)
> 
> As far as the primed version filterAlpha' is concerned, f is a global 
> symbol. The argument list is just used for values that vary.
> 
> This will be your friend if the parameter list starts to stack up with 
> more and more "reference" or "environment" inputs. It is also easier to 
> see that f never changes if it is defined in only one spot.
> 
> Later, when you study monads you will notice the same pattern in the 
> Reader monad, where the technique is even more valuable.
> 
> Dan Weston
> 
> Alexteslin wrote:
>> 
>> filterAlpha :: (a -> Bool) -> [a] -> [a]
>> filterAlpha f [] = []
>> filterAlpha f (x:xs)
>> 	|f x   = x : filterAlpha f xs    -- corrected
>> 	|otherwise = filterAlpha f xs    -- corrected
> 
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> 

-- 
View this message in context: http://www.nabble.com/filterFirst-tf4131377.html#a11790806
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.



More information about the Haskell-Cafe mailing list