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: please critique my first stab at systems programming
(Sean Perry)
2. Re: please critique my first stab at systems programming
(Felipe Almeida Lessa)
3. Re: please critique my first stab at systems programming
(Daniel Fischer)
4. Re: Copy record with mutations? (Chadda? Fouch?)
----------------------------------------------------------------------
Message: 1
Date: Thu, 14 Apr 2011 08:29:25 -0700
From: Sean Perry <[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=us-ascii
On Apr 14, 2011, at 6:17 AM, Felipe Almeida Lessa wrote:
> 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.
>
Well, I started with B.findSubstring, but the compiler told me that was
deprecated and break was the preferred replacement. Looking at the docs,
findSubstring was shown to be the equivalent of the case statement which you
see in my code.
I do not care how many times the pattern appears in a sector just whether or
not it exists at all.
>
> 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:
>
Now that I am a little more awake, let me state my requirements more clearly to
ensure this works as I expect.
I am hunting through the raw sectors of the disk looking for a sector with a
specific pattern. "PART" is just a standin for the real thing. Since disks are
obviously bigger than a meg or two I need to worry about space usage. Time
performance is important but not as important as not reading all 256G of a disk
into memory (-:
I need to output the sector number when found. This is why I started with the
very simple sector by sector search instead of larger chunk based one. In my
experience the OS tends to read in 4k multiples even when you just ask for 512
so there is a buffer somewhere helping with performance.
I like your approach Felipe. Thanks. Definitely not something that would have
been obvious from reading the library docs. I knew there was a better way
hiding somewhere though. Had I been coding in Python or some other language I
typically use I would have let Handle be the point of abstraction. Anything
that looked like a File would work for me.
The definition of 'go' looks nice and elegant. What does 'the $!i here in 'go
$! i+1' do?
------------------------------
Message: 2
Date: Thu, 14 Apr 2011 13:19:03 -0300
From: Felipe Almeida Lessa <[email protected]>
Subject: Re: [Haskell-beginners] please critique my first stab at
systems programming
To: Sean Perry <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Thu, Apr 14, 2011 at 12:29 PM, Sean Perry <[email protected]> wrote:
> I like your approach Felipe. Thanks. Definitely not something that would have
> been obvious from reading the library docs. I knew there was a better way
> hiding somewhere though. Had I been coding in Python or some other language I
> typically use I would have let Handle be the point of abstraction. Anything
> that looked like a File would work for me.
The Yesod Book contains a chapter about the enumerator package [1],
although it isn't finished, yet.
[1] http://www.yesodweb.com/book/enumerator
> The definition of 'go' looks nice and elegant. What does 'the $!i here in 'go
> $! i+1' do?
As you know, f $ x is just f x. The $! operator, however, evaluates
the second argument to weak head normal form (WHNF) before calling the
function. In other words,
f $! x = x `seq` f x
If we just called "go (i+1)", a huge thunk would be created. For
example, if the string was found in sector 1024, then it would return
Just (((((???(1+1)???+1)+1)+1)+1)+1)
instead of simply Just 1024. But, when we call "go $! i+1", we force
the sum to be done *now*, so the funtion would return
Just (1023+1)
which doesn't cause any problems =).
Cheers,
--
Felipe.
------------------------------
Message: 3
Date: Thu, 14 Apr 2011 18:21:34 +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 17:29:25, Sean Perry wrote:
> Now that I am a little more awake, let me state my requirements more
> clearly to ensure this works as I expect.
>
> I am hunting through the raw sectors of the disk looking for a sector
> with a specific pattern. "PART" is just a standin for the real thing.
> Since disks are obviously bigger than a meg or two I need to worry
> about space usage. Time performance is important but not as important
> as not reading all 256G of a disk into memory (-:
That's why you'd use a *lazy* ByteString or an Enumerator/Iteratee.
You can have a space leak or leak a file handle with lazy ByteStrings if
you hold on to parts of the ByteString unnecessary or the compiler doesn't
detect that the file handle isn't referenced later anymore, but in simple
situations that is unlikely. So in simple situations using the simpler code
you get from lazy ByteStrings is reasonable.
I suspect you can also cause leaks with Enumerators and Iteratees, but
that'll be harder, since avoiding those is the/one point of Enumerators and
Iteratees.
>
> I need to output the sector number when found. This is why I started
> with the very simple sector by sector search instead of larger chunk
> based one. In my experience the OS tends to read in 4k multiples even
> when you just ask for 512 so there is a buffer somewhere helping with
> performance.
But I think you'd still have a system call with a context switch every 512
bytes, while if you read in larger chunks you'll have fewer context
switches and better performance.
>
> I like your approach Felipe. Thanks. Definitely not something that would
> have been obvious from reading the library docs. I knew there was a
> better way hiding somewhere though. Had I been coding in Python or some
> other language I typically use I would have let Handle be the point of
> abstraction. Anything that looked like a File would work for me.
>
> The definition of 'go' looks nice and elegant. What does 'the $!i here
> in 'go $! i+1' do?
($!) is strict application, 'go $! i+1' evaluates (i+1) before doing the
work in go itself, thus preventing the building of a huge thunk
go ((...(0+1)...+1)+1)
------------------------------
Message: 4
Date: Thu, 14 Apr 2011 20:14:04 +0200
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] Copy record with mutations?
To: Andrew n marshall <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Apr 13, 2011 at 5:22 PM, Andrew n marshall
<[email protected]> wrote:
>
> Is there a syntax or function to copy a record, but with select fields
> populated with new values?
> For example, extending LYAH's Car
>
> let?stang67?=?Car?{company="Ford",?model="Mustang",?year=1967}
> let stang68 = stang67 `mutate` Car {year=1968}
>
The classic solution is simply :
> let stang68 = stang67 {year=1968}
but that is not very convenient (not at all in fact) when for instance
you have several nested records or you want to base the "new" value of
a field on its "old" value. Thus was invented the concept of
"functional references" or "lenses" which extend this mechanism by a
more complete and convenient protocol to copy a record with
modification on some of its field.
See
http://hackage.haskell.org/package/fclabels
http://www.cis.upenn.edu/~bcpierce/papers/newlenses-popl.pdf
There are plenty of lenses packages on Hackage, I don't know which are
more used, maybe some that uses them could help here ?
--
Jeda?
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 34, Issue 26
*****************************************