Concurrent Prime Generator

后端 未结 10 705
终归单人心
终归单人心 2020-12-20 20:32

I\'m going through the problems on projecteuler.net to learn how to program in Erlang, and I am having the hardest time creating a prime generator that can create all of the

10条回答
  •  旧时难觅i
    2020-12-20 21:16

    The 'badarity' error means that you're trying to call a 'fun' with the wrong number of arguments. In this case...

    %%L = for(1,N, fun() -> spawn(fun(I) -> wait(I,N) end) end),

    The for/3 function expects a fun of arity 1, and the spawn/1 function expects a fun of arity 0. Try this instead:

    L = for(1, N, fun(I) -> spawn(fun() -> wait(I, N) end) end),
    

    The fun passed to spawn inherits needed parts of its environment (namely I), so there's no need to pass it explicitly.

    While calculating primes is always good fun, please keep in mind that this is not the kind of problem Erlang was designed to solve. Erlang was designed for massive actor-style concurrency. It will most likely perform rather badly on all examples of data-parallel computation. In many cases, a sequential solution in, say, ML will be so fast that any number of cores will not suffice for Erlang to catch up, and e.g. F# and the .NET Task Parallel Library would certainly be a much better vehicle for these kinds of operations.

提交回复
热议问题