Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Randomness, lists, and unfoldr (Felipe Lessa)
   2. Re:  Randomness, lists, and unfoldr (Alex Rozenshteyn)
   3. Re:  List Sorting and I/O (Lorenzo Isella)


----------------------------------------------------------------------

Message: 1
Date: Mon, 13 Sep 2010 20:34:16 -0300
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] Randomness, lists, and unfoldr
To: Alex Rozenshteyn <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Mon, Sep 13, 2010 at 7:53 PM, Alex Rozenshteyn <[email protected]> wrote:
> Is there a way to take a given monad's bind and wrap it to make it more
> lazy?  It doesn't seem like there should be, but I'm being hopeful.

I wouldn't bother with that.  Just write the following and be happy =).

iterateM :: (Monad m) => Int -> (a -> m a) -> a -> m [a]
iterateM 0 _ _ = return []
iterateM n act start = do
   next <- act start
   rest <- iterateM (n-1) act next
   return (start : rest)

However, I think the answer of your question is "no".  And in cases
where you can do something similar (e.g. in IO you can use
unsafeInterleaveIO), most of the time it isn't the best solution.

Cheers!

-- 
Felipe.


------------------------------

Message: 2
Date: Mon, 13 Sep 2010 19:53:45 -0400
From: Alex Rozenshteyn <[email protected]>
Subject: Re: [Haskell-beginners] Randomness, lists, and unfoldr
To: Felipe Lessa <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

The reason I ask is that I plan on making a simulation, which will run until
the user decides to pause.

Potentially, I could keep state and update it, but that doesn't seem
haskelly.  With an infinite list, I even get history for free.

On Mon, Sep 13, 2010 at 7:34 PM, Felipe Lessa <[email protected]>wrote:

> On Mon, Sep 13, 2010 at 7:53 PM, Alex Rozenshteyn <[email protected]>
> wrote:
> > Is there a way to take a given monad's bind and wrap it to make it more
> > lazy?  It doesn't seem like there should be, but I'm being hopeful.
>
> I wouldn't bother with that.  Just write the following and be happy =).
>
> iterateM :: (Monad m) => Int -> (a -> m a) -> a -> m [a]
> iterateM 0 _ _ = return []
> iterateM n act start = do
>   next <- act start
>   rest <- iterateM (n-1) act next
>   return (start : rest)
>
> However, I think the answer of your question is "no".  And in cases
> where you can do something similar (e.g. in IO you can use
> unsafeInterleaveIO), most of the time it isn't the best solution.
>
> Cheers!
>
> --
> Felipe.
>



-- 
          Alex R
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100913/7d0426cf/attachment-0001.html

------------------------------

Message: 3
Date: Tue, 14 Sep 2010 11:40:55 +0200
From: Lorenzo Isella <[email protected]>
Subject: Re: [Haskell-beginners] List Sorting and I/O
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed


>> Finally, I would like to be able to save the two lists as plain text
>> files (no commas, no brackets).
>> I tried writeFile+Show, but this saves the lists exactly as printed on
>> screen (whereas I simply would like a text file showing a column of
>> numbers when opened by your favorite editor).
>> Many thanks
>
> One list as a column:
>
> writeFile "bar" $ unlines (map show list)
>
> A list as a space-separated sequence of numbers on one line:
>
> writeFile "bar" $ unwords (map show list)

Hi Daniel,
And thanks for your help. Your solution works, but it looks like that on 
my machine (an up-to-date desktop computer with plenty of ram) file 
writing is unusually slow [it looks like it takes more than 10 minutes 
to write a text file of a few megabites which comes from a long list].
I wonder if there is any way to speed up the process or it is an 
inherent limitation of lists (or I am making some mistake).
Cheers

Lorenzo

P.S.: the list is a list of lists, hence I use this function

save_vector_flat filename list = writeFile filename $ unlines (map show 
$ concat list)


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 27, Issue 32
*****************************************

Reply via email to