Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When working with lists a all elements are often of similar datatype or meaning. You seldom see lists like <code>["John Doe","1970-01-01","London"]</code> but rather <code>#person{name="John Doe",...}</code> or even {"John Doe",...}. To change a value in a record and tuple: </p> <pre><code>-record(person,{name,born,city}). f(#person{}=P) -&gt; P#person{city="New City"}. % record f({_,_,_,}=Tuple) -&gt; erlang:setelement(3,Tuple,"New City"). % tuple </code></pre> <p>This might not solve anything for your particular problem. To take your own example in comment: </p> <pre><code>f1([H1,H2,_H3,H4,H5],E) -&gt; [H1,H2,E,H4,H5]. </code></pre> <p>If you give a more specific description of the environment and problem it's easier to under which solution might work the best.</p> <p>Edit: One (rather bad) solution 1. </p> <pre><code>replacenth(L,Index,NewValue) -&gt; {L1,[_|L2]} = lists:split(Index-1,L), L1++[NewValue|L2]. 1&gt; replacenth([1,2,3,4,5],3,foo). [1,2,foo,4,5] </code></pre> <p>Or slightly more efficient depending on the length of your lists.</p> <pre><code>replacenth(Index,Value,List) -&gt; replacenth(Index-1,Value,List,[],0). replacenth(ReplaceIndex,Value,[_|List],Acc,ReplaceIndex) -&gt; lists:reverse(Acc)++[Value|List]; replacenth(ReplaceIndex,Value,[V|List],Acc,Index) -&gt; replacenth(ReplaceIndex,Value,List,[V|Acc],Index+1). </code></pre> <p>Even better is my function f1 above but maybe, just maybe the problem is still located as discussed above or <a href="https://stackoverflow.com/questions/4370756/replace-an-element-at-an-index-in-a-list-in-erlang">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload