[erlang-questions] Matrix rotator

Richard Carlsson richardc@REDACTED
Sun Jun 14 22:01:04 CEST 2009


Tony Arcieri wrote:
> I saw a fairly interesting problem posted on this week's Ruby Quiz, a matrix
> rotator:
> 
> http://osdir.com/ml/ruby-talk/2009-06/msg01127.html
> 
> I thought this problem might also be interesting to an Erlang audience as
> well.
> 
> The goal is to rotate an arbitrary sized input matrix 90 degrees
> counterclockwise,

I don't have the time right now, but this function could serve as an
inspiration: it doesn't quite do what you want - it transposes rather
than rotates the matrix:

   %% Transpose rows and columns in a list of lists. Works even if
   %% sublists are not of same length. Empty sublists are stripped.

   transpose([]) -> [];
   transpose([[] | Xss]) -> transpose(Xss);
   transpose([[X | Xs] | Xss]) ->
     [[X | [H || [H | T] <- Xss]]
      | transpose([Xs | [T || [H | T] <- Xss]])].

I think I stole it from the Haskell standard library.

     /Richard


More information about the erlang-questions mailing list