[erlang-questions] Generators?

tsuraan tsuraan@REDACTED
Mon Jul 30 16:56:59 CEST 2007


While reading the Erlang and Neural Networks paper, I noticed that the
created dot product function was (in my eye) not a terribly good example of
functional programming.  A more "functional style" would be something like:

dot_prod(L1, L2) -> lists:sum(lists:zipwith(fun(X,Y) -> X*Y end, L1, L2)).

So we're basically mapping a function across a pair of lists, and then
summing it.  This looks pretty inefficient though; I don't think you can do
any better than the function given in the paper, so I'm just
trying to satisfy my curiosity here.  The most offensive thing that I see
about my function is that it creates a temporary list of products, and then
sums that.  If L1 and L2 are large, itwill also create a large temporary
list that is a total waste.  In python, you could write a zipwith function
as a generator that yields values as they are requested.  Any standard
function that takes a list will also take a generator because they both just
expose the looping interface (__iter__).  Is it possible to create a
generator function in erlang that can be used with the list functions?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20070730/dd27e0cc/attachment.htm>


More information about the erlang-questions mailing list