[erlang-questions] Re: [erlang-questions 41] Re: A sudoku solver in Erlang compared to Python

Kresten Krab Thorup krab@REDACTED
Tue Mar 29 13:40:35 CEST 2011


Hi,

On Mar 28, 2011, at 15:22 , Andreas Pauley wrote:

> I'd love to use atoms as square names, just because it looks cleaner.
> However, I'm getting a severe performance hit when I use
> list_to_atom/1 on each square name.
> 
> Can you show me how you did it in conjunction with ct_expand?

These are the changed functions, [and then the unit tests need to be updated accordingly]...  ct_expand doesn't like function applications, so the function unitlist() needs to have the other definitions inlined.

Kresten


squares() ->
    %% Returns a list of 81 square names, including "a1" etc.
    ct_expand:term([erlang:list_to_atom([X,Y]) || X <- ?rows, Y <- ?cols]).
col_squares() ->
    %% All the square names for each column.
    ([[erlang:list_to_atom([X,Y]) || X <- ?rows, Y <- [C]] || C <- ?cols]).
row_squares() ->
    %% All the square names for each row.
    ct_expand:term([[erlang:list_to_atom([X,Y]) || X <- [R], Y <- ?cols] || R <- ?rows]).
box_squares() ->
    %% All the square names for each box.
    ct_expand:term([[erlang:list_to_atom([X,Y]) || X <- R, Y <- C] ||
                       R <- ["abc", "def", "ghi"],
                       C <- ["123", "456", "789"]]).

unitlist() ->
    %% A list of all units (columns, rows, boxes) in a grid.
    ct_expand:term(  [[erlang:list_to_atom([X,Y]) || X <- ?rows, Y <- [C]] || C <- ?cols]
                     ++ [[erlang:list_to_atom([X,Y]) || X <- [R], Y <- ?cols] || R <- ?rows]
                     ++ [[erlang:list_to_atom([X,Y]) || X <- R, Y <- C] ||
                            R <- ["abc", "def", "ghi"],
                            C <- ["123", "456", "789"]]).




More information about the erlang-questions mailing list