lib_misc.erl:
-module(lib_misc).
-export([for/3]).
for(Max, Max, F) -> [F(Max)];
for(I, Max, F) -> [F(I)|for(I+1, Max, F)].
then in erl:
…
5> lib_misc:for(1,10,fun(I) -> I end).
[1,2,3,4,5,6,7,8,9,10]
6> lib_misc:for(1,10,fun(I) -> I*I end).
[1,4,9,16,25,36,49,64,81,100]
Weird. But check this out:
1> Fruit = [apple,pear,orange].
[apple,pear,orange]
2> MakeTest = fun(L) -> (fun(X) -> lists:member(X, L) end) end.
#Fun<erl_eval.6.56006484>
3> IsFruit = MakeTest(Fruit).
#Fun<erl_eval.6.56006484>
4> IsFruit(pear).
true
5> IsFruit(apple).
true
6> IsFruit(dog).
false
7> lists:filter(IsFruit, [dog,orange,cat,apple,bear]).
[orange,apple]
That is extremely cool. I’ve got to make this a higher priority, so that I can get past the baby talk.
