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:  Processing a list of files the Haskell way (Lorenzo Bolla)
   2. Re:  A question about an infinite type (Lorenzo Bolla)


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

Message: 1
Date: Wed, 21 Mar 2012 17:13:39 +0000
From: Lorenzo Bolla <[email protected]>
Subject: Re: [Haskell-beginners] Processing a list of files the
        Haskell way
To: Yawar Amin <[email protected]>
Cc: [email protected]
Message-ID:
        <CADjgTRz5Mw69Sd90R=WwQ=1dgCcz6BV_h3=jshjzsa+9ea8...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

What about something as simple as this?


import           Control.Monad (forM)
import           System.Directory (doesDirectoryExist, getDirectoryContents)
import           System.FilePath ((</>))
import qualified Data.ByteString as B
import           Data.Digest.OpenSSL.MD5 (md5sum)
import qualified Data.Map as M

getRecursiveContents :: FilePath -> IO [FilePath]
getRecursiveContents topdir = do
        names <- getDirectoryContents topdir
        let properNames = filter (`notElem` [".", ".."]) names
        paths <- forM properNames $ \name -> do
                let path = topdir </> name
                isDirectory <- doesDirectoryExist path
                if isDirectory
                        then getRecursiveContents path
                        else return [path]
        return (concat paths)

getMD5 :: FilePath -> IO String
getMD5 file = md5sum `fmap` B.readFile file

main :: IO ()
main = do
        files <- getRecursiveContents "."
        md5s <- sequence $ map getMD5 files
        let m = M.fromListWith (++) $ zip md5s [[f] | f <- files]
        putStrLn $ M.showTree m


The biggest part is the "getRecursiveContent", shamelessly stolen from RWH.

L.




On Sun, Mar 18, 2012 at 5:43 PM, Yawar Amin <[email protected]> wrote:

> 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
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120321/e652c225/attachment-0001.htm>

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

Message: 2
Date: Thu, 22 Mar 2012 09:53:39 +0000
From: Lorenzo Bolla <[email protected]>
Subject: Re: [Haskell-beginners] A question about an infinite type
To: "Costello, Roger L." <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID:
        <CADjgTRza0zNa2XAfagfJt2b5h67X=b+0qcjycbrzogwhbme...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

You can find your answer here:
http://en.wikibooks.org/wiki/Haskell/YAHT/Type_advanced



On Tue, Feb 28, 2012 at 12:52 PM, Costello, Roger L. <[email protected]>wrote:

> Hi Folks,
>
> Here is an interesting phenomena:
>
> let f = (\x y -> x y x)
>
> let p = (\x y -> 1)
>
> Now let's evaluate  f p 1
>
> f p 1  = (\x y -> x y x) p 1                -- by replacing f with its
> definition
>
>          = p 1 p                                        -- by substituting
> x with p and y with 1
>
>          = (\x y -> 1) 1 p                     -- by replacing the first p
> with its definition
>
>          = 1                                               -- the function
> returns 1 regardless of its arguments
>
> However, Haskell will not proceed with that evaluation because it will
> first determine the
>
> type signature of f and judge it to be an infinite type.
>
> Conversely, if f is omitted and this expression
>
>    p 1 p
>
> is evaluated then the result 1 is generated.
>
> Why does f p 1 (which evaluates to p 1 p) fail whereas p 1 p succeeds?
>
> What lesson should I learn from this example?
>
> /Roger
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20120322/e53474bc/attachment-0001.htm>

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

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


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

Reply via email to