| Author |
Message |
< Erlang ~ Changing element in list? |
| pottageb |
Posted: Sun Jan 23, 2011 7:58 pm |
|
|
|
Joined: 23 Jan 2011
Posts: 1
|
Is there any way of editing a specific element in a list, say the 2nd one?
Thanks |
|
|
| Back to top |
|
| rvirding |
Posted: Mon Jan 24, 2011 11:08 am |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
|
| Back to top |
|
| ultranewb |
Posted: Mon Jan 24, 2011 2:50 pm |
|
|
|
User
Joined: 10 Jan 2011
Posts: 10
|
Something I find odd is on the Erlang course on the website http://www.erlang.org/course/sequential_programming.html :
* Used to store a variable number of items.
* Lists are dynamically sized.
Yet apparently there cannot be true if I understand the language correctly. Once you have constructed a list, it cannot be changed, no?
Further down on the same web page:
Variables can only be bound once! The value of a variable can never be changed once it has been set (bound). |
|
|
| Back to top |
|
| rvirding |
Posted: Mon Jan 31, 2011 5:53 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
Well, yes and no depending on how you look at it. A tuple like
{a,b,c,d,e}
is one object so if you want to change one of the elements or add a new element you have to build a completely new tuple containing the old and new elements. A list like
[a,b,c,d,e]
is actually a linked chain of pairs, cons cells, where each one contains one of the elements of the list and a link to the next cons cell. Each cell is immutable but you can step down the list and add new cells to the beginning of the list. It is in this that lists are dynamically sized.
The erlang syntax for a cons cell is [Head|Tail] where the Head is used for the list element and the Tail for the next cons cell. So the above list is really:
[a|[b|[c|[d|[e|[]]]]]]
([] is the empty list, nil, which marks the end of the list). Seeing lists are used so much and the above syntax is cumbersome there is a short form for this which is as I first wrote the list. But they are exactly equivalent, it is purely syntactic sugar, but a very useful one. So the syntax [Head|Tail] can either mean "construct a new list taking the list in the variable Tail and adding a new element Head to it" or if it is a pattern "match the value for a cons cell and put the first element in the variable Head and the rest of the list in the variable Tail".
Immutability of variables and immutability of data are really two separate things, Erlang has both. In Erlang variables are really just references to values (data) and aren't real objects as such. All it means that in a function clause once a Variable references a value, is bound to it, it will always reference that value, it cannot be bound to another value. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| niglas |
Posted: Wed Mar 02, 2011 1:59 pm |
|
|
|
User
Joined: 31 Jan 2011
Posts: 12
|
If I got it right, there is no way you can achieve this kinda operation, even if you are using lists instead of tuples:
Code: 1> A = [a,b,c,d,e]. % A = [a,b,c,d,e]
2> lists:add(f,A). % A = [a,b,c,d,e,f]
Nor:
Code: 1> B = [b,c,d,e]. % B = [b,c,d,e]
2> lists:push(a,B). % B = [a,b,c,d,e]
Right? |
|
|
| Back to top |
|
| rvirding |
Posted: Sat Mar 05, 2011 12:03 am |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
I think there is some confusion here. Neither of these two functions exist in lists and even if they did they would both neither modify the original list nor variables referring to them:
niglas wrote: Code: 1> A = [a,b,c,d,e]. % A = [a,b,c,d,e]
2> lists:add(f,A). % A = [a,b,c,d,e,f] Here the intent seems to be to add an element to the end of the list. If this was done a new list would be created (as data can't be changed) and A would still point to the old list.
niglas wrote: Code: 1> B = [b,c,d,e]. % B = [b,c,d,e]
2> lists:push(a,B). % B = [a,b,c,d,e] Here an element would be prepended to the list without rebuilding the list but B would still refer to the old list. Once a variable is bound to a value it can not be rebound. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| niglas |
Posted: Mon Mar 07, 2011 9:15 am |
|
|
|
User
Joined: 31 Jan 2011
Posts: 12
|
Great, so I had it right
niglas wrote: ...there is no way you can achieve this kinda operation... |
|
|
| Back to top |
|
| timrila |
Posted: Tue Jun 12, 2012 8:56 am |
|
|
|
User
Joined: 28 Mar 2012
Posts: 32
|
|
| Back to top |
|
| goods |
Posted: Fri Jun 29, 2012 2:05 am |
|
|
|
User
Joined: 28 Jun 2012
Posts: 14
|
| Christian Louboutin Homme Espadrilles est absolument représentatif de la mode et les loisirs, christian Chaussures Louboutin, est devenu une question de cours le protagoniste populaire de la saison. Nous ferons de notre mieux pour vous satisfaire. Toutes les Louboutin Pas Cher a été travaillé par des artisans et des espadrilles bonne Christian Louboutin pour les Christian Louboutin Homme par la main et c'est le résultat d'une sélection rigoureuse. si vous êtes engagé dans la vogue ou si vous voulez garder le même rythme avec la tendance, Louboutin 2012 chaussures de sport des hommes pourrait être votre meilleur choix. Commandez-le maintenant produits sont expédition libre. |
|
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 5:41 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|