Traversing graphs
Tony Rogvall
<>
Wed May 25 20:09:38 CEST 2005
25 maj 2005 kl. 15.50 skrev Vlad Dumitrescu:
> Hi all,
>
> I just hit a problem where I clearly see how simple it would be
> with lazy evaluation. But since we don't have that, I wonder if
> anyone could point me to some non-lazy algorithms.
>
By working a bit, lazy evaluation can be done... (WARNING do not use
in real apps ;-)
For example using the macros:
-define(delay(X), fun() -> (X) end).
-define(force(X), force((X))).
force(F) when function(F) -> F();
force(V) -> V.
You can define a lazy algorithms (can of course only be used in this
context)
zeros() ->
[0 | ?delay(zeros())].
ones() ->
[1 | ?delay(ones())].
twos() ->
[2 | ?delay(ones())].
seq(Start) ->
[Start | ?delay(seq(Start+1))].
sieve1(L, P) ->
[N | T1] = ?force(L),
case N rem P of
0 -> sieve1(T1,P);
_ -> sieve([N | ?delay(sieve1(T1,P))])
end.
sieve(L) ->
[P | X] = ?force(L),
[P | ?delay(sieve1(X, P))].
primes() ->
sieve(seq(2)).
Regards
/Tony
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20050525/de582853/attachment.html>
More information about the erlang-questions
mailing list