
If your code performs a common task (such as conversion, accumulation,
testing), then you should use higher-level constructs than a for loop.
Using map, filter, foldr, and foldl will make it easier to write
correct code.
If you'd like to imitate a for loop exactly -- that is, to perform
some action multiple times -- it's very easy to create a pure
function. We do not have to stoop to mutable variables.
-------------------------------------------------
for :: Monad m => a -> (a -> Bool) -> (a -> a) -> (a -> m ()) -> m ()
for start test step body = loop start where
loop x = if test x
then body x >> loop (step x)
else return ()
main = for 0 (< 100) (+ 1) $ \i -> do
-- do something with i
print i
-------------------------------------------------
On Tue, Mar 9, 2010 at 16:25, zaxis
In FP the variable can not be changed once created. Yes, it has much advantage . However, i feel it is too strict. As we know, the local variable is allocated on stack which is thread safe.
So if the local variable can be changed, then we can use loop, etc. same as imperative languages. For example, for (i=0; i<100; i++) where `i` is a local variable in function.
Any suggestion is appreciated!
----- fac n = let { f = foldr (*) 1 [1..n] } in f -- View this message in context: http://old.nabble.com/If-the-local-variable-can-be-changed-...-tp27844016p27... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe