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

Edwin Fine <>
Sun Jul 6 23:14:53 CEST 2008


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)]

?

2008/7/6 Richard Carlsson <>:

> Toby Thain wrote:
>
>> Hi list,
>>
>> I stumbled on this simple 'puzzle'[0] posted some years ago on
>>  comp.lang.tcl (originally to an Icon mailing list).
>>
>> After solving it in Icon[1] and SQL[2] I decided to see what an  Erlang
>> solution would look like. I'm posting here firstly because  other people's
>> solutions would be interesting, and also I'd like to  see how my own
>> solution[3] could be improved.
>>
>> Any takers over the weekend? :-)
>>
>> --Toby
>>
>> [0] http://groups.google.com/group/comp.lang.tcl/msg/8846d9f7491ba0ba
>> [1] http://telegraphics.com.au/svn/puzzles/trunk/vier-neun/vn.icn
>> [2] http://telegraphics.com.au/svn/puzzles/trunk/vier-neun/Makefile
>> [3] http://telegraphics.com.au/svn/puzzles/trunk/vier-neun/erlang/
>>
>
> Here's my version. Note that it's often better to think in terms of
> stages of list processing (mapreduce etc.), instead of inserting and
> looking up in dictionaries. I tag each generated pair with the E
> element, just to make the list easier to work with later.
>
>    /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 there is a unique solution (VIER,NEUN) for
> %% some particular E.
>
> it() ->
>    Qs = [integer_to_list(S) || S <- [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].
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
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/20080706/653f3acd/attachment.html>


More information about the erlang-questions mailing list