for (int i = 1; i != 10; ++i) printf("Hello, World!\n");
Thank you very much!
That would really be doing homework. If you don't understand how to do
that, you didn't understand how Haskell handles side effects and
sequencing.
Here is one of a thousand ways to do it:
printHello :: IO ()
printHello = forM_ [1..10] (\_ -> putStrLn "Hello, World!")
Although it does contain the familiar "for" word, it doesn't have much
to do with your loop. Go ahead and learn.
Regards,
Ertugrul.
> So far as I know, there is not loop statement in haskell, so how can I
> write a corresponding code in haskell like below:
>
> for (int i = 1; i != 10; ++i) printf("Hello, World!\n");
>
> Thank you very much!
I suspect that something like,
main = sequence_ $ replicate 10 (putStrLn "Hello, World!")
should work for you. At
http://www.haskell.org/onlinereport/standard-prelude.html
you can see `replicate' as,
take :: Int -> [a] -> [a]
take n _ | n <= 0 = []
take _ [] = []
take n (x:xs) = x : take (n-1) xs
repeat :: a -> [a]
repeat x = xs where xs = x:xs
replicate :: Int -> a -> [a]
replicate n x = take n (repeat x)
and you can also see how sequence_ is defined.
Two things are very well worth doing:
Understand that lists and accumulators tend to be used in Haskell where
loops are often used in other languages. This can still allow fast code:
for instance, see http://www.cse.unsw.edu.au/~dons/papers/CLS07.html
Study Prelude.hs (which may already be installed on your system with
your Haskell compiler).
Mark
One approach (there will be many):
> let printn n str = putStr (unlines $ replicate n str)
> println 4 "Arved"
> "Arved"
> "Arved"
> "Arved"
> "Arved"
Tested with ghci, GHC 6.8.2 on Windows. General approach is, you need N
copies of the string, so use "replicate" to make them. Use "unlines" to add
linefeeds. Display using "putStrLn".
AHS
> main = sequence_ $ replicate 10 (putStrLn "Hello, World!")
I should add that I had in mind Arne's previous thread about the meaning
of, xs = [printChar 'a', printChar 'b']
I think of it as a list of IO actions, and in that context I think of
`sequence'-like stuff as a way to glue them together to make one action
that does them all. (For monads other than IO, of course the `gluing
together' can take different forms and I get by well enough with the
idea that it's about composing computations, but for simple use of IO
one needn't get into that.)
Mark
As a rule of thumb, you usually replace loops either by list processing
(map, fold, etc.), or by explicit tail recursion.
> so how can I write a corresponding code in haskell like below:
> for (int i = 1; i != 10; ++i) printf("Hello, World!\n");
That's maybe not the best example for a loop, because it requires
output, which is a side-effect. If you want to do IO in Haskell, you
essentially go back to imperative thinking. As pure Haskell is not
imperative, you use the do-construct, which in this case will make sure
that the imperative part happens inside the IO-Monad:
import Control.Monad
main = do
forM_ [1..10] $ \i -> do
print "Hello World"
You can also print the loop variable:
main = do
forM_ [1..10] $ \i -> do
putStrLn $ "i=" ++ show i
But that's NOT how you normally use loops in Haskell. For example, if you
want to add all the i's, as in
s = 0;
for (int i = 1; i <= 10; i++) {
s += i
}
you would instead use a fold:
s = foldl (+) 0 [1..10]
Or in this case, you can use "sum", which is defined in the Prelude:
sum xs = foldl (+) 0 xs
s = sum [1..10]
If you're new to Haskell, it's maybe easier to first work with examples
that don't use input or output, so don't bother about "do" and "forM_". In
real Haskell programs, you often have an extra (small) "top layer" that
does the input and output, and the "real work" then gets done in purely
functional style. This is different from what you're used to in C etc.,
and it takes a while to digest it.
I'd recommend to read up about fold and map, and do some examples with
them.
HTH,
- Dirk
Thank you all of you!!!! I have found many ways to answer my
questions, that's wonderful^_^