Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.haskell

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 04-08-2008, 02:17 PM
һǧÍߵĵ¶ÀÇ
Guest
 
Posts: n/a
Default How to repeatedly calling a function

Hi all,
Maybe this is a stupid question, but I really want to get your help.
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!
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-08-2008, 03:10 PM
Ertugrul Söylemez
Guest
 
Posts: n/a
Default Re: How to repeatedly calling a function

一�瓦的刀狼 <knifewolf@gmail.com> wrote:

> Maybe this is a stupid question, but I really want to get your help.
> 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");


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.


--
http://ertes.de/

Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 03:34 PM
Mark T.B. Carroll
Guest
 
Posts: n/a
Default Re: How to repeatedly calling a function

һǧÍߵĵ¶ÀÇ <knifewolf@gmail.com> writes:

> 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/...d-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
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 03:38 PM
Arved Sandstrom
Guest
 
Posts: n/a
Default Re: How to repeatedly calling a function

"??????" <knifewolf@gmail.com> wrote in message
news:18f48931-386a-4545-a8da-b7a53132a907@k1g2000prb.googlegroups.com...
> Hi all,
> Maybe this is a stupid question, but I really want to get your help.
> 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!


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


Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 03:47 PM
Mark T.B. Carroll
Guest
 
Posts: n/a
Default Re: How to repeatedly calling a function

"Mark T.B. Carroll" <Mark.Carroll@Aetion.com> writes:

> 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
Reply With Quote
  #6 (permalink)  
Old 04-09-2008, 09:16 AM
Dirk Thierbach
Guest
 
Posts: n/a
Default Re: How to repeatedly calling a function

?????? <knifewolf@gmail.com> wrote:
> Hi all,
> Maybe this is a stupid question, but I really want to get your help.
> So far as I know, there is not loop statement in haskell,


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


Reply With Quote
  #7 (permalink)  
Old 04-18-2008, 01:49 PM
һǧÍߵĵ¶ÀÇ
Guest
 
Posts: n/a
Default Re: How to repeatedly calling a function

On 4ÔÂ8ÈÕ, ÏÂÎç10ʱ17·Ö, һǧÍߵĵ¶ÀÇ <knifew...@gmail.com> wrote:
> Hi all,
> Maybe this is a stupid question, but I really want to get your help.
> 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!


Thank you all of you!!!! I have found many ways to answer my
questions, that's wonderful^_^
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Function reference with versions ? Choate, Paul@DDS Newsgroup comp.soft-sys.sas 0 03-20-2006 04:59 PM
Re: How to build Dynamic Variable names and values Arthur Tabachneck Newsgroup comp.soft-sys.sas 0 02-11-2006 06:34 PM
Re: How to build Dynamic Variable names and values SUBSCRIBE SAS-L Chandra Gadde Newsgroup comp.soft-sys.sas 0 02-11-2006 03:50 PM



All times are GMT. The time now is 01:17 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.