Deduct "New List" with "new Value" only for some "specific Tag", from Old List, using "List Comprehension".

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Sun Jul 19 13:59:21 CEST 2020


Hi!

Your question looks like a typical X-Y problem, as described in:
http://xyproblem.info/

FWIW, your particular problem can be solved with list comprehensions, but
require a helper function, as Oliver described:

clamp_length({length, V}) when V > 255 -> {length, 255};
clamp_length(Otherwise) -> Otherwise.

t() ->
    T = [{a,1}, {length,24568}, {b,2}, {c,3}, {length, 54741}],
    [clamp_length(X) || X <- T].

But I'm guessing this is not a good solution to your real problem. Chances
are you are better off breaking your long list into smaller parts first,
then process each subpart on its own. Generally, the structure of the data
should be followed by your recursive descent, as you are really writing a
parser here. As you get more special cases, one large function pattern
match isn't going to cut it. If you split it up, it gets much easier to
handle and the code will also become more readable if you think a bit about
the naming of the functions.

So in short: what are you *really* trying to do?


On Sun, Jul 19, 2020 at 12:55 PM Papa Tana <papa.tana101@REDACTED> wrote:

> I had to do it like below:
>
> T = [{a,1}, {length,24568}, {b,2}, {c,3}, {length, 54741}].
>
> WithoutLength = [ {Tag, Value} || {Tag, Value} <-T, Tag/= length].
> HowManyLength = length([ {Tag, Value} || {Tag, Value} <-T, Tag== length]).
> Extra = lists:duplicate(HowManyLength, {length, 255}).
>
> >NewT = [WithoutLength | Extra ].
> [[{a,1},{b,2},{c,3}],{length,255},{length,255}]
>
> Not really the expected result, yes all values of tag length are
> replaced, but not in the right order.
>


-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20200719/f3f62835/attachment.htm>


More information about the erlang-questions mailing list