[erlang-questions] Sunday puzzle - trying to improve my Erlang

Edwin Fine erlang-questions_efine@REDACTED
Mon Jul 7 06:23:55 CEST 2008


On Sun, Jul 6, 2008 at 5:39 PM, Richard Carlsson <richardc@REDACTED> wrote:

> Edwin Fine wrote:
>
>> Richard,
>>
>> Lovely solution! It's stuff like this that helps newcomers to Erlang like
>> myself to learn how to write better Erlang.
>>
>> I'm curious, though; what is the reason for writing
>>
>> Qs = [integer_to_list(S) || S <- [X*X || X <- lists:seq(32,99)]]
>>
>> instead of
>>
>> Qs = [integer_to_list(X*X) || X <- lists:seq(32,99)]
>>
>
> None whatsoever. The person responsible should be keelhauled.
> Oh, right... Sorry.
>
> It's just a remnant of a refactoring. I started out working on
> lists on the form [S div 1000, ..., S rem 10], but realized that
> I could just use the lists of character codes instead. But I
> didn't tidy up after changing to integer_to_list(S). Improved
> version attached.


Oh, thank goodness!!  I thought it was some expert optimization or technique
that I just couldn't understand :)


>    /Richard
>
> %% File: solve.erl
> %% @author Richard Carlsson
>
> -module(solve).
> -export([it/0]).
>
> %% Problem: VIER and NEUN are 4-digit squares; determine distinct V, I,
> %% E, R, N, and U, such that (given the choice of E) there exists a
> %% unique solution (VIER,NEUN).
>
> it() ->
>    Qs = [integer_to_list(X*X) || X <- lists:seq(32,99)],
>    Ps = [{E,{Vr,Nn}} || [N,E,_U,N]=Nn <- Qs, [_V,_I,E1,_R]=Vr <- Qs,
>                         E =:= E1,
>                         length(ordsets:from_list(Nn++Vr)) =:= 6],
>    D = lists:foldl(fun ({E,_}, D) -> dict:update_counter(E,1,D) end,
>                    dict:new(), Ps),
>    [P || {E,1} <- dict:to_list(D), {E1,P} <- Ps, E=:=E1].
>
>


-- 
The great enemy of the truth is very often not the lie -- deliberate,
contrived and dishonest, but the myth, persistent, persuasive, and
unrealistic. Belief in myths allows the comfort of opinion without the
discomfort of thought.
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080707/3f7bc391/attachment.htm>


More information about the erlang-questions mailing list