[Haskell-beginners] please critique my first stab at systems programming

Felipe Almeida Lessa felipe.lessa at gmail.com
Thu Apr 14 18:19:03 CEST 2011


On Thu, Apr 14, 2011 at 12:29 PM, Sean Perry <shaleh at speakeasy.net> 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.



More information about the Beginners mailing list