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.  please critique my first stab at systems programming (Sean Perry)
   2. Re:  How to extract from Maybe & Either in GHCi
      (Ertugrul Soeylemez)
   3. Re:  please critique my first stab at systems     programming
      (Daniel Fischer)
   4. Re:  please critique my first stab at systems     programming
      (Felipe Almeida Lessa)
   5. Re:  please critique my first stab at systems     programming
      (Daniel Fischer)
   6. Re:  please critique my first stab at systems programming (MAN)
   7. Re:  please critique my first stab at systems     programming
      (Felipe Almeida Lessa)


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

Message: 1
Date: Thu, 14 Apr 2011 03:14:59 -0700
From: Sean Perry <[email protected]>
Subject: [Haskell-beginners] please critique my first stab at systems
        programming
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

The idea is to walk the disk looking for a signature, say NTFS or EXT. Since we 
do not know where the block containing this identifier is, we read the blocks 
in one at a time.

Long term I would like to support command line arguments for the name of the 
file and the offset to start looking.

Comments on style, idioms, etc. welcomed. I am particularly interested in the 
hIsEOF check and if there is a better way to handle that.

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import IO
import System.IO

chunkSize = 512

searchForPattern handle pat = searchForPattern' 0 handle pat                    
   

searchForPattern' index handle pat = do
    eof <- hIsEOF handle
    if eof then return Nothing
      else do
        bytes <- B.hGet handle chunkSize
        case BC.breakSubstring pat bytes of
          (x, y) | BC.null y -> searchForPattern' (index + 1) handle pat
                 | otherwise -> return (Just index)

main = do 
    fromHandle <- openBinaryFile "blocks" ReadMode
    result <- searchForPattern fromHandle (BC.pack "PART")
    case result of
      Nothing -> putStr "Not Found.\n"
      Just n  -> putStr $ "Found at " ++ show n ++ ".\n"
    hClose fromHandle
    putStr "Done.\n"




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

Message: 2
Date: Thu, 14 Apr 2011 13:12:15 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] How to extract from Maybe & Either in
        GHCi
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Christopher Done <[email protected]> wrote:

> FWIW there is also Data.Either which has the either function.

Only few people seem to realize that 'maybe' and 'either' exist and how
useful they are.  They are defined in the Prelude, so no imports are
necessary.

  either this that . maybe defValue foo $ someMaybeEitherValue

They also help with monadic code:

  getStuff :: IO (Maybe String)

  getStuff >>= maybe (throwIO (MyError "Stuff not available")) putStrLn


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/





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

Message: 3
Date: Thu, 14 Apr 2011 13:50:26 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] please critique my first stab at
        systems programming
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="iso-8859-1"

On Thursday 14 April 2011 12:14:59, Sean Perry wrote:
> The idea is to walk the disk looking for a signature, say NTFS or EXT.
> Since we do not know where the block containing this identifier is, we
> read the blocks in one at a time.

It may be more efficient to read the entire thing into a lazy ByteString 
instead of reading in very small strict chunks.
That also makes the eof check unnecessary.

> 
> Long term I would like to support command line arguments for the name of
> the file and the offset to start looking.
> 
> Comments on style, idioms, etc. welcomed. I am particularly interested
> in the hIsEOF check and if there is a better way to handle that.

$ cabal install stringsearch
-- faster searches for a pattern than provided by bytestring

import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Search as L

blockSize = 512

main = do
   stuff <- L.readFile "blocks"
   case L.indices (BC.pack "PART") stuff of
     [] -> putStrLn "Not Found."
     (i:_) -> putStrLn $ "Found at " ++ show (i `quot` blockSize) ++ "."
   putStrLn "Done."

> 
> import qualified Data.ByteString as B
> import qualified Data.ByteString.Char8 as BC
> import IO
> import System.IO
> 
> chunkSize = 512
> 
> searchForPattern handle pat = searchForPattern' 0 handle pat
> 
> searchForPattern' index handle pat = do
>     eof <- hIsEOF handle
>     if eof then return Nothing
>       else do
>         bytes <- B.hGet handle chunkSize
>         case BC.breakSubstring pat bytes of
>           (x, y) | BC.null y -> searchForPattern' (index + 1) handle pat
> 
>                  | otherwise -> return (Just index)
> 
> main = do
>     fromHandle <- openBinaryFile "blocks" ReadMode
>     result <- searchForPattern fromHandle (BC.pack "PART")
>     case result of
>       Nothing -> putStr "Not Found.\n"
>       Just n  -> putStr $ "Found at " ++ show n ++ ".\n"
>     hClose fromHandle
>     putStr "Done.\n"
> 



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

Message: 4
Date: Thu, 14 Apr 2011 09:30:59 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] please critique my first stab at
        systems programming
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On Thu, Apr 14, 2011 at 8:50 AM, Daniel Fischer
<[email protected]> wrote:
> $ cabal install stringsearch
> -- faster searches for a pattern than provided by bytestring
>
> import qualified Data.ByteString.Char8 as BC
> import qualified Data.ByteString.Lazy as L
> import qualified Data.ByteString.Lazy.Search as L
>
> blockSize = 512
>
> main = do
> ? stuff <- L.readFile "blocks"
> ? case L.indices (BC.pack "PART") stuff of
> ? ? [] -> putStrLn "Not Found."
> ? ? (i:_) -> putStrLn $ "Found at " ++ show (i `quot` blockSize) ++ "."
> ? putStrLn "Done."

This solves a different problem.  The original looked for PART only on
the beginning of each block, while your solution will find everything
that has PART in it.  Because this is I/O bound, probably the real
time taken won't be very different, but the program will surely use
more CPU time and report more false positives.

Cheers,

-- 
Felipe.



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

Message: 5
Date: Thu, 14 Apr 2011 14:50:54 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] please critique my first stab at
        systems programming
To: Felipe Almeida Lessa <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain;  charset="utf-8"

On Thursday 14 April 2011 14:30:59, Felipe Almeida Lessa wrote:
> On Thu, Apr 14, 2011 at 8:50 AM, Daniel Fischer
> 
> <[email protected]> wrote:
> > $ cabal install stringsearch
> > -- faster searches for a pattern than provided by bytestring
> > 
> > import qualified Data.ByteString.Char8 as BC
> > import qualified Data.ByteString.Lazy as L
> > import qualified Data.ByteString.Lazy.Search as L
> > 
> > blockSize = 512
> > 
> > main = do
> >   stuff <- L.readFile "blocks"
> >   case L.indices (BC.pack "PART") stuff of
> >     [] -> putStrLn "Not Found."
> >     (i:_) -> putStrLn $ "Found at " ++ show (i `quot` blockSize) ++
> > "." putStrLn "Done."
> 
> This solves a different problem.  The original looked for PART only on
> the beginning of each block,

Well, I would have expected that, but

> searchForPattern' index handle pat = do
> 
>     eof <- hIsEOF handle
>     if eof then return Nothing
>     
>       else do
>       
>         bytes <- B.hGet handle chunkSize
>         case BC.breakSubstring pat bytes of
>         
>           (x, y) | BC.null y -> searchForPattern' (index + 1) handle pat
>           
>                  | otherwise -> return (Just index)

searches the entire block and checks whether the second component is empty 
to see whether there's any match at all. So I thought that'd be the desired 
behaviour.

If one wants to only look at the start of the blocks, isPrefixOf  would be 
a much more efficient way than breakSubstring. It would probably be still 
more efficient to read larger chunks than 512 bytes and step through them 
with 512-byte steps to check for a "PART" there (unless the "PART" appears 
in the first few blocks).

> while your solution will find everything
> that has PART in it.  Because this is I/O bound, probably the real
> time taken won't be very different, but the program will surely use
> more CPU time and report more false positives.
> 
> Cheers,



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

Message: 6
Date: Thu, 14 Apr 2011 10:02:44 -0300
From: MAN <[email protected]>
Subject: Re: [Haskell-beginners] please critique my first stab at
        systems programming
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <1302786164.2383.2.camel@dy-book>
Content-Type: text/plain; charset="UTF-8"

searchForPattern' (and therefore searchForPattern) will read the entire
file, indeed. To check only the first 'n' chunks in a file you'd have to

1) receive a lazy bytestring, check first n*chuckSize bytes.
2) receive the handle, loop over n chunks.

El jue, 14-04-2011 a las 14:50 +0200, Daniel Fischer escribi?:
> On Thursday 14 April 2011 14:30:59, Felipe Almeida Lessa wrote:
> > On Thu, Apr 14, 2011 at 8:50 AM, Daniel Fischer
> > 
> > <[email protected]> wrote:
> > > $ cabal install stringsearch
> > > -- faster searches for a pattern than provided by bytestring
> > > 
> > > import qualified Data.ByteString.Char8 as BC
> > > import qualified Data.ByteString.Lazy as L
> > > import qualified Data.ByteString.Lazy.Search as L
> > > 
> > > blockSize = 512
> > > 
> > > main = do
> > >   stuff <- L.readFile "blocks"
> > >   case L.indices (BC.pack "PART") stuff of
> > >     [] -> putStrLn "Not Found."
> > >     (i:_) -> putStrLn $ "Found at " ++ show (i `quot` blockSize) ++
> > > "." putStrLn "Done."
> > 
> > This solves a different problem.  The original looked for PART only on
> > the beginning of each block,
> 
> Well, I would have expected that, but
> 
> > searchForPattern' index handle pat = do
> > 
> >     eof <- hIsEOF handle
> >     if eof then return Nothing
> >     
> >       else do
> >       
> >         bytes <- B.hGet handle chunkSize
> >         case BC.breakSubstring pat bytes of
> >         
> >           (x, y) | BC.null y -> searchForPattern' (index + 1) handle pat
> >           
> >                  | otherwise -> return (Just index)
> 
> searches the entire block and checks whether the second component is empty 
> to see whether there's any match at all. So I thought that'd be the desired 
> behaviour.
> 
> If one wants to only look at the start of the blocks, isPrefixOf  would be 
> a much more efficient way than breakSubstring. It would probably be still 
> more efficient to read larger chunks than 512 bytes and step through them 
> with 512-byte steps to check for a "PART" there (unless the "PART" appears 
> in the first few blocks).
> 
> > while your solution will find everything
> > that has PART in it.  Because this is I/O bound, probably the real
> > time taken won't be very different, but the program will surely use
> > more CPU time and report more false positives.
> > 
> > Cheers,
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners





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

Message: 7
Date: Thu, 14 Apr 2011 10:17:13 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] please critique my first stab at
        systems programming
To: Daniel Fischer <[email protected]>,  Sean Perry
        <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

On Thu, Apr 14, 2011 at 9:50 AM, Daniel Fischer
<[email protected]> wrote:
> Well, I would have expected that, but
[snip]
> searches the entire block and checks whether the second component is empty
> to see whether there's any match at all. So I thought that'd be the desired
> behaviour.

Oops, you're right =).  I wonder what was OP's intended behaviour.


On Thu, Apr 14, 2011 at 7:14 AM, Sean Perry <[email protected]> wrote:
> The idea is to walk the disk looking for a signature, say NTFS or EXT.
> Since we do not know where the block containing this identifier is, we
> read the blocks in one at a time.

You may use an iteratee as well, which gives clean, efficient code
that doesn't depend on IO (among other things).  Using the
"enumerator" package [1], (and assuming that you just want to check
the beginning of each block), you could write:

import qualified Data.Enumerator as E
import qualified Data.Enumerator.Binary as EB

searchForPattern :: Monad m => B.ByteString -> E.Iteratee B.ByteString
m (Maybe Int)
searchForPattern pat | restLen >= 0 = go 0
                     | otherwise    = return Nothing
    where
      patL    = L.fromChunks [pat]
      patLen  = B.length pat
      restLen = chunkSize - patLen
      go i = do str <- EB.take (fromIntegral patLen)
                case (L.length str < fromIntegral patLen, str == patL) of
                  (True, _) -> return Nothing
                  (_, True) -> return (Just i)
                  _         -> do EB.drop (fromIntegral restLen)
                                  go $! i+1

Using lazy bytestrings could make your code leak memory, leak handles
and/or choke with exceptions.  Using handles directly in IO (as in
your original approach) makes code harder to test and leaves all
gritty details up to you (such as reading blocks larger than 512
bytes).  This iteratee is easily testable with pure code, does not
leak, handles exceptions gracefully and you don't have to worry about
how the file is being read.

I've attached a full working example.  In the example I read in blocks
of 16 KiB, but you may easily adjust that (without compromising
correctness).

Cheers! =)

[1] http://hackage.haskell.org/package/enumerator

-- 
Felipe.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SeanPerry.hs
Type: text/x-haskell
Size: 1420 bytes
Desc: not available
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110414/70af3de9/attachment.hs>

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

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


End of Beginners Digest, Vol 34, Issue 25
*****************************************

Reply via email to