Nth element of a Tuple

Bengt Kleberg bengt.kleberg@REDACTED
Fri Apr 7 13:32:05 CEST 2006


On 2006-04-07 13:21, Eric Shun wrote:
> I would like to write a function that replaces the Nth element of a tuple.
> exemple:
>  
> replace(3,{a,b,c,d},a)   returns {a,b,a,c,d}
>  
> how can I do that?

erlang:setelement(Index, Tuple1, Value) -> Tuple2

               Types  Index = 1..size(Tuple1)
                      Tuple1 = Tuple2 = tuple()
                      Value = term()

               Returns a tuple which is a copy of the argument Tuple1 
with  the
               element  given  by the integer argument Index (the first 
element
               is the element with index 1) replaced by the argument Value.

               >setelement(2, {10, green, bottles}, red).

               {10, red, bottles}


bengt



More information about the erlang-questions mailing list