Why is Erlang crashing on large sequences?

前端 未结 4 1636
失恋的感觉
失恋的感觉 2021-02-06 13:50

I have just started learning Erlang and am trying out some Project Euler problems to get started. However, I seem to be able to do any operations on large sequences without cra

4条回答
  •  伪装坚强ぢ
    2021-02-06 14:15

    Your OS may have a default limit on the size of a user process. On Linux you can change this with ulimit.

    You probably want to iterate over these 64000000 numbers without needing them all in memory at once. Lazy lists let you write code similar in style to the list-all-at-once code:

    -module(lazy).
    -export([seq/2]).
    
    seq(M, N) when M =< N ->
        fun() -> [M | seq(M+1, N)] end;
    seq(_, _) ->
        fun () -> [] end.
    
    1> Ns = lazy:seq(1, 64000000).
    #Fun
    2> hd(Ns()).
    1
    3> Ns2 = tl(Ns()).
    #Fun
    4> hd(Ns2()).
    2
    

提交回复
热议问题