[Haskell-beginners] First code review

Brent Yorgey byorgey at seas.upenn.edu
Sun Feb 24 01:27:13 CET 2013

On Sun, Feb 24, 2013 at 12:15:05AM +0100, Emanuel Koczwara wrote:
> Hi,
> Dnia 2013-02-23, sob o godzinie 17:35 -0500, Brent Yorgey pisze:
> > On Sat, Feb 23, 2013 at 04:39:53PM +0100, Emanuel Koczwara wrote:
> > > Hi,
> > > 
> > > Note: Following code is a solution for a problem from hackerrank.com
> > > (Category: Artifical Intelligence / Single Player Games / Bot saves
> > > princess).
> > 
> > Looks pretty good overall.  One note is that using lists of lists for
> > Grid and Heuristic will be slow, especially Heuristic since you do
> > lots of repeated lookups.  For small grids it really doesn't make much
> > difference, but if you wanted to run it on larger grids you might
> > notice.  Since both the Grid and Heuristic values are created once and
> > then used in a read-only fasion, this is a perfect opportunity to use
> > arrays: see 
> > 
> >   http://hackage.haskell.org/packages/archive/array/latest/doc/html/Data-Array.html
> > 
> > Using read-only arrays is really quite simple (as opposed to
> > read/write arrays which require a monad of some sort).
> > 
>   Thank you, I will try Arrays. Handling list of lists is very hard for
> me. Please look at this code: http://hpaste.org/82925 (indexedGrid and
> getDirty). In C/C++ it's very natural for me, here it looks like i'm
> missing something.

In C/C++ you get to use arrays with indexing.  Lists are entirely
different, so it's no surprise that something natural in C/C++ should
feel foreign here.  Indeed, I don't think lists are the right data
structure for you to be using.  If you use arrays I think this code
will become much simpler too.

A good rule of thumb is that lists should be primarily used as a
*control structure* (i.e. to describe something that would be done
with a loop in an imperative language).  If you find yourself using
lists as a *data structure*, especially a random-access one, you
probably ought to be using something else instead (Data.Sequence,
arrays, ...)


More information about the Beginners mailing list