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.  State monad and destructive updates (Rohit Garg)
   2. Re:  Processing a list of files the Haskell way (Yawar Amin)
   3. Re:  State monad and destructive updates (Ertugrul S?ylemez)
   4. Re:  State monad and destructive updates (Brent Yorgey)


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

Message: 1
Date: Sun, 18 Mar 2012 13:34:30 -0400
From: Rohit Garg <[email protected]>
Subject: [Haskell-beginners] State monad and destructive updates
To: [email protected]
Message-ID:
        <cac1t7gilc4kopdy1wu-6xf4qt-hysm_fxz7uedujcfgf3o1...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

As I have been trying to learn the monads a bit more, I have come to
realize that State monad doesn't actually represent state. It just
provides a convenient glue for threading state. Is that correct? If
so, then for performance using state monad is as good as not using it
at all. Is there a way to have state with destructive update?

Thanks,

-- 
Rohit Garg

http://rpg-314.blogspot.com/

Graduate Student
Applied and Engineering Physics
Cornell University



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

Message: 2
Date: Sun, 18 Mar 2012 17:43:50 +0000 (UTC)
From: Yawar Amin <[email protected]>
Subject: Re: [Haskell-beginners] Processing a list of files the
        Haskell way
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

Hi Michael,

Michael Schober <Micha-Schober <at> web.de> writes:

> [...]
> I took the liberty to modify the output a little bit to my needs - maybe 
> a future reader will find it helpful, too. It's attached below.

I kind of played around with your example a little bit and wondered if it
could be implemented in terms of just the basic Haskell Platform
modules and functions. So as an exercise I rolled my own directory
traversal and duplicate finder functions. This is what I came up with:

- walkDirWith: walks a given directory with a given function that takes a
Handle to any (unknown type) value, and returns association lists of
paths and the unknown type values.

- filePathMap: I think roughly analogous to your duplicates function.

- main: In the third line of the main function, I use hFileSize as an
example of a function that takes a Handle to an IO value, in this case IO
Integer. A hash function could easily be put in here. The last line
pretty-prints the Map in a tree-like format.

import System.IO
import System.Environment (getArgs)
import System.Directory ( doesDirectoryExist
                        , getDirectoryContents)
import Control.Monad (mapM)
import Control.Applicative ((<$>))
import System.FilePath ((</>))
import qualified Data.Map as M

walkDirWith :: FilePath -> (Handle -> IO r) -> IO [(r, FilePath)] ->
               IO [(r, FilePath)]
walkDirWith path f walkList = do
  isDir <- doesDirectoryExist path
  if isDir
    then do
      paths <- getDirectoryContents path
      concat <$> mapM (\p -> walkDirWith (path </> p) f walkList)
                      [p | p <- paths, p /= ".", p /= ".."]
    else do
      rValue <- withFile path ReadMode f
      ((:) (rValue, path)) <$> walkList

filePathMap :: Ord r => [(r, FilePath)] -> M.Map r [FilePath]
filePathMap pathPairs =
  foldl (\theMap (r, path) -> M.insertWith' (++) r [path] theMap)
        M.empty
        pathPairs

main :: IO ()
main = do
  [dir] <- getArgs
  fileSizes <- walkDirWith dir hFileSize $ return []
  putStr . M.showTree $ filePathMap fileSizes

Obviously there's no right or wrong way to do it, but I'm wondering
what you think.

Regards,

Yawar





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

Message: 3
Date: Sun, 18 Mar 2012 20:21:37 +0100
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] State monad and destructive updates
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"

Rohit Garg <[email protected]> wrote:

> As I have been trying to learn the monads a bit more, I have come to
> realize that State monad doesn't actually represent state. It just
> provides a convenient glue for threading state. Is that correct? If
> so, then for performance using state monad is as good as not using it
> at all. Is there a way to have state with destructive update?

There are things like STRefs and ST arrays.  However, note that you only
need destructive update if you have to update only a relatively small
portion of a strict (!) data structure (like individual elements in an
array).  In all other cases just go with a state monad or recursion.
You won't lose performance there.

Most pure data structures don't count here.  For example updating maps,
sets or other ADT-based data structures does not need to copy anything.
It just updates its references and is done.  The penalty is a small
constant or logarithmic factor.

This is one reason why you would generally prefer nonstrict boxed data
structures.  Nonstrict boxing makes slower imperative code, but faster
functional code.

Often when your intuition tells you that you need destructive update for
performance, it will be wrong.  Remember that Haskell is a lazy
language.  If you are unsure how to solve a particular problem purely,
free free to ask.


Greets,
Ertugrul

-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120318/d4b99cde/attachment-0001.pgp>

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

Message: 4
Date: Sun, 18 Mar 2012 16:02:52 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] State monad and destructive updates
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sun, Mar 18, 2012 at 01:34:30PM -0400, Rohit Garg wrote:
> Hi,
> 
> As I have been trying to learn the monads a bit more, I have come to
> realize that State monad doesn't actually represent state. It just
> provides a convenient glue for threading state. Is that correct? 

Correct.

> If
> so, then for performance using state monad is as good as not using it
> at all. Is there a way to have state with destructive update?

Yes, see the ST monad.

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad-ST.html
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-STRef.html
http://hackage.haskell.org/packages/archive/array/latest/doc/html/Data-Array-ST.html

-Brent



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

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


End of Beginners Digest, Vol 45, Issue 24
*****************************************

Reply via email to