| Author |
Message |
< Erlang ~ learning erlang |
| f00biebletch |
Posted: Wed Nov 14, 2007 12:53 am |
|
|
|
Joined: 13 Nov 2007
Posts: 3
|
I am yet another java/c#/ruby engineer teaching myself erlang, and, am trying to write a fun to do polynomial multiplication. I can take 2 tuple lists representing a polynomial (tuples being terms like 3x^2) and am down to a list like this:
Code:
[{term,1,x,2},{term,2,x,1},{term,2,x,1},{term,4,x,0}]
where element 2 is the coefficient, 3 is the var name, and 4 is the exponent. I just need to group like terms now, "like" being the same exponent.
I can (and have) folded the list with a dict and aggregated by finding and storing the term with the exponent for the key, but that seems less than optimal.
Are there any elegant list comprehension solutions out there?
Thanks, this is really a vibrant site that helps a ton with Erlang. |
|
|
| Back to top |
|
| francesco |
Posted: Wed Nov 14, 2007 9:41 am |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
I don't get your question, but will try to reply anyhow. Are you trying to order something like
Code: [{term,7,x,0},{term,2,x,2},{term,2,x,1},{term,4,x,3}]
to something like
Code: [{term,4,x,3},{term,2,x,2},{term,2,x,1},{term,7,x,0}]
If so, use lists:reverse(lists:keysort(4,List)).
If the above is not what you are looking for, could you pls give a before and after example.
Francesco
--
http://www.erlang-consulting.com |
|
|
| Back to top |
|
| f00biebletch |
Posted: Wed Nov 14, 2007 1:34 pm |
|
|
|
Joined: 13 Nov 2007
Posts: 3
|
Sorry for the confusion, I'm trying to get from
Code:
[{term,1,x,2},{term,2,x,1},{term,2,x,1},{term,4,x,0}]
to
Code:
[{term,1,x,2},{term,4,x,1},{term,4,x,0}]
The last element of the tuple is the exponent, so I am just adding the coefficients of terms with like exponents. If I had something like:
Code:
[{term,5,x,2},{term,1,x,2},{term,2,x,0},{term,4,x,0}]
I would want to get to
Code:
[{term,6,x,2},{term,6,x,0}]
Hope that helps clarify, thanks. |
|
|
| Back to top |
|
| francesco |
Posted: Thu Nov 15, 2007 8:43 am |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
Try
Code:
F = fun({term,Val,x,Exp}, [{term,Val2,x,Exp}|Tail]) ->
[{term, Val + Val2,x,Exp}|Tail];
({term,Val,x,Exp}, Acc) ->
[{term,Val,x,Exp}|Acc]
end.
L = lists:keysort(4,[{term,1,x,2},{term,2,x,1},{term,2,x,1},{term,4,x,0}]).
lists:foldl(F, hd(L), tl(L)).
Regards,
Francesco
--
http://www.erlang-consulting.com |
|
|
| Back to top |
|
| f00biebletch |
Posted: Thu Nov 15, 2007 4:47 pm |
|
|
|
Joined: 13 Nov 2007
Posts: 3
|
| Brilliant, I should have suspected pattern matching would be the key, I like the "peek" functionality by splitting the "rest" of the list as well. Many thanks for your time and help. |
|
|
| 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
|
|
|