Let's first focus on the turn. For an atom [a, b, c, d]
, calling grid 2
on it for printing yields
a b
c d
Turning that 90° clockwise would result in
c a
d b
which comes from the list [c, a, d, b]
. So a clockwise turn isn't a cyclic swapping of list elements. If only 2×2 atoms would need to be considered, the natural implementation of turn
using a flat list would be
turn [a,b,c,d] = [c,a,d,b]
turn _ = error "Not an atom"
But, according to the overview, things are not that simple, you can sew quilts, so you can get quilts of any dimension m×n
where both m
and n
are even. So using a flat list representation for quilts is not the best idea.
Suppose you represented quilts as a list of lists, each row one list, so for example
[ [a,b,c,d]
, [e,f,g,h] ]
for a 2×4
quilt. Rotating that 90° clockwise yields the 4×2
quilt
[ [e,a]
, [f,b]
, [g,c]
, [h,d] ]
Now, there's nothing in the standard libraries that does that directly, but, in Data.List
, we have transpose
, which transforms the 2×4
quilt above into
[ [a,e]
, [b,f]
, [c,g]
, [d,h] ]
and we're then halfway there:
turn = map reverse . transpose
According to the overview, when turning, one would also need to rotate symbols, '\'
becoems '/'
and vice versa, '-'
becomes '|'
and vice versa. That would be achieved by mapping aturnChar :: Char -> Char
function over all rows.