-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlazy2.erl
115 lines (79 loc) · 2.42 KB
/
lazy2.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-module(lazy2).
-compile([export_all]).
% Unsuccessful experiement to use a hash of an
% expression as index in a lookup table/ETS table.
-define(cons(X,Xs,Tab), begin ets:insert(Tab, {?ref(X,Xs), {thunk, fun () -> {X,Xs} end}}),
io:format("done cons insert~n"),
{ref,?ref(X,Xs)} end).
-define(ref(X,Xs), splat({??X,??Xs})).
% -define(test(T),begin io:format("??T is ~w~n",[??T]),
% io:format("length(??T) is ~w~n",[length(??T)]),
% io:format("splat(??T) is ~w~n",[splat(??T)]) end ).
% test() -> ?test(primes({})).
% cons(X,Xs) ->
% fun() -> {X,Xs} end
next_ref(Tab) ->
[{0,Ref}]=ets:lookup(Tab,0),
Ref.
nil() -> {}.
head({ref,Ref},Tab) ->
io:format("entered head~n"),
case ets:lookup(Tab,Ref) of
[{Ref,{thunk,F}}] -> Val = F(),
ets:insert(Tab,{Ref,Val}),
{H,_} = Val,
H;
[{Ref,{H,_}}] -> io:format("head success~n"),
H
end.
tail({ref,Ref},Tab) ->
io:format("entered tail~n"),
case ets:lookup(Tab,Ref) of
[{Ref,{thunk,F}}] -> Val = F(),
ets:insert(Tab,{Ref,Val}),
{_,T} = Val,
T;
[{Ref,{_,T}}] -> io:format("tail success~n"),
T
end.
% list1() ->
% fun() ->
% {23,
% fun() -> 1/0 end}
% end.
%ones() ->
% cons(1,ones()).
ones(Tab) ->
?cons(1,ones(Tab),Tab).
ns(N,Tab) ->
?cons(N,ns(N+1,Tab),Tab).
primes(Tab) -> sieve(ns(2,Tab),Tab).
% sieve(Ns) ->
% H = head(Ns),
% ?cons(H,sieve(cull(H,tail(Ns)))).
sieve(Ns,Tab) ->
H = head(Ns,Tab),
?cons(H,sieve(cull(H,tail(Ns,Tab),Tab),Tab),Tab).
cull(N,Ns,Tab) ->
H = head(Ns,Tab),
case H rem N of
0 -> cull(N,tail(Ns,Tab),Tab);
_ -> ?cons(H,cull(N,tail(Ns,Tab),Tab),Tab)
end.
index(N,Ls,Tab) ->
case N of
0 -> head(Ls,Tab);
_ -> index(N-1,tail(Ls,Tab),Tab)
end.
addZip(Xs,Ys,Tab) ->
?cons(head(Xs,Tab)+head(Ys,Tab), addZip(tail(Xs,Tab),tail(Ys,Tab),Tab),Tab).
fibs(Tab) ->
?cons(0,
?cons(1,
addZip(fibs(Tab),tail(fibs(Tab),Tab),Tab),Tab),Tab).
ps(_Xs,0,_T) -> ok;
ps(Xs,N,Tab) ->
io:format("~w~n",[head(Xs,Tab)]),
ps(tail(Xs,Tab),N-1,Tab).
splat(F) ->
crypto:hash(md5,binary:bin_to_list(term_to_binary(F))).