[Haskell-beginners] Mutable grid
mike h
mike_k_houghton at yahoo.co.uk
Mon Dec 19 18:31:27 UTC 2016
Thanks for the pointers - I’ll take a look.
The background to this is one of the puzzles on Advent Of Code 2016 Q.8.
https://adventofcode.com/2016/day/8 <https://adventofcode.com/2016/day/8>
There are (several hundred) sequential operations on a grid 50 x 6 - initially all zeroes
e.g.
rotate row y=0 by 4
rect 2x1 — sets sub grid from (0,0) to (2,1) to all 1s
rotate column x=35 by 1
I’m fine about parsing the input to a data structure and executing them i.e.
evalExpr :: Expr -> Screen -> Screen — screen is essentially [[Int]]
evalExpr e s =
case e of
(Rect r c ) -> evalRect r c s
(RotRow r by) -> evalRotRow r by s
(RotCol c by) -> evalRotCol c by s
(NOP ) -> id s
rotating a row was simple enough, code to rotate column a bit untidy and not very nice. The
evalRect - which sets values to one in the rectangle of size r x c starting at (0,0) top left - triggered the original question.
At this point my knowledge of Haskell is being pushed (which is good) but I have a feeling that
my approach is not ‘correct’ once it gets beyond the parsing. Should each of the evalRect, evalRotRow and evalRotCol be called with a Screen (i.e. the grid at the root of this question)?
Is the state monad a fit for this problem?
Should I change my approach or is using vector the way forward?
Many thanks
Mike
> On 19 Dec 2016, at 15:27, Michael Orlitzky <michael at orlitzky.com> wrote:
>
> On 12/19/2016 08:10 AM, mike h wrote:
>> Hi,
>>
>> I’m looking a problem where I have an NxN grid of ints. I need a
>> function like setValue x y newVal
>>
>> I have tried using [[Int]] but it does become messy when splitting ,
>> dropping and then ++ back together.
>>
>> What other options are available to represent a mutable grid?
>>
>
> Mutable vectors (from the vector[1] package) are an obvious choice. When
> I had to do something similar, I wound up going all the way to repa[2],
> which magically turns all of your grid operations into parallel ones.
>
>
> [1] https://hackage.haskell.org/package/vector
> [2] https://hackage.haskell.org/package/repa
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161219/110a31b6/attachment.html>
More information about the Beginners
mailing list