[Haskell-beginners] Feedback on my first, small project (piece).

David McBride toad3k at gmail.com
Tue Aug 5 20:42:39 UTC 2014


I took a look at your main and made some changes that might help.  I have
not looked at your glob file at all.

1.  Rather than using length args successively in multiple if/else if
statements, which recalculates the value, just use a case, calculate it
once and then go from there, when you can.

2. Use let or where to make new function rather than putting them all
inline.

3. Instead of looping the main entirely to get all of stdin, use said new
function and have it loop itself.  You could also streamline this to have
its own function that does not require the glob to be passed into each loop.

4. Use withFile instead of opening the file and then forgetting about the
handle.  If you had enough files on the commandline, you would exhaust the
allowable open file handles on the system.  Withfile will close the handle
when you are done with each one.

5. Use mapM_ to get rid of the results that are returned by mapM.  It is
also faster if you are not going to use the results anyways.

6. When you are in a monad (such as IO), you can use when instead of if to
optionally perform a statement, and it looks a bit prettier.

There are other minor things, but this is a pretty good start.

Here's the resulting code (I have not run it, but it should be pretty close
to what you had): https://gist.github.com/anonymous/615e48004ca2eed82d0a


On Tue, Aug 5, 2014 at 4:08 PM, Dominik Bollmann <dominikbollmann at gmail.com>
wrote:

>
> Hello Haskeller's,
>
> I recently dived into Haskell and then wanted to practice it a bit!
> Therefore I wrote a small program that matches UNIX-style globs. The
> program behaves kind of like grep, just that it matches a glob and not a
> regular expression. And also, it offers only *very* rudimental
> functionality compared to grep.
>
> The code is available on github: https://github.com/bollmann/Globber
> When writing the program I tried to satisfy the specification as given
> at: http://www.scs.stanford.edu/14sp-cs240h/labs/lab1.html. This Lab
> material btw. also inspired me to try writing such a program :-).
>
> In order to improve my programming in Haskell, I would love to hear
> feedback from you guys on this first small project of mine. Any comments
> regarding style, used idioms, as well as general and specific code
> improvements are highly appreciated. Thanks!
>
> Cheers, Dominik.
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140805/d8509743/attachment.html>


More information about the Beginners mailing list