[erlang-questions] memoization (was: Re: [Fwd: Re: Functional Programming])

Bob Ippolito <>
Fri Aug 31 19:14:23 CEST 2007


If you use pure Erlang data structures you could only cache results
during the tail recursion (by adding another parameter, perhaps behind
the scenes with a parse transform or macro). Since ets is mutable and
lasts longer than the function call, you can cache results for the
duration of the process. Also, the performance of pure Erlang
dict/gb_trees/etc. is not generally as good as ets.

ets is generally preferred over the process table, as far as I have seen.

-bob

On 8/31/07, Colin Meyer <> wrote:
> Hi,
>
> Thanks for your reply. I hadn't gotten to learning about ETS yet. I
> guess that this is fairly similar to my idea of caching results in the
> process dictionary.
>
> Is it a correct understanding that caching is not practical with "pure"
> Erlang data structures?
>
> Thanks,
> -Colin.
>
> On Fri, Aug 31, 2007 at 11:24:52AM -0500, Matthew O'Gorman wrote:
> > i have this
> > memo.erl:
> > -module (memo).
> > -export ([y/1, memoize/1, fib/1]).
> >
> > y (F) ->
> >     F (fun (X) ->    (y (F)) (X) end).
> >
> > memoize (Tab, F) ->
> >     fun (B) ->
> >             fun (C) ->
> >                     case ets:lookup (Tab, C) of
> >                         [] ->
> >                             R = (F (B)) (C),
> >                             ets:insert (Tab, {C, R }),
> >                             R;
> >                         [{C, R}] ->
> >                             R
> >                     end
> >             end
> >     end.
>
> [...]
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list