[erlang-questions] Replace list element

Richard O'Keefe ok@REDACTED
Fri May 27 04:08:14 CEST 2011


On 27/05/2011, at 2:26 AM, Alexander Kuleshov wrote:

> Hello,
> 
> How can i fast replace 2 elements in Erlang list? For example I have
> list: [1,2,3,4], how can i quickly get [1,3,2,4]?

First:  how do you know which elements to replace?
        By their value?  By their index?  By satisfying some
        property?  By some summary of what precedes them?
Second: how do you know what to replace them with?
        Are they fixed values, or are they computed?

This may seem silly, but

[if is_integer(X), 1 < X, X < 4 -> 5-X
  ; true                        -> X
 end || X <- [1,2,3,4]
]

will handle the example you gave, so what is wrong with that answer?





More information about the erlang-questions mailing list