|
|
Orchid XP v7 wrote:
> Darren New wrote:
>
>> I thought it was pretty cool that the latest Erlang compilers will see
>> code like
>>
>> map(F, [H|T]) -> [F(H)|map(F,T)];
>> map(F, []) -> [].
>>
>> and turn it into the same code you'd get with an auxiliary accumulator
>> and a call to "reverse" at the end.
>
> Doesn't reverse require N operations? Wouldn't that mean that map now
> takes 2N operations instead of just N?
Probably. But note that map as I wrote it isn't tail recursive, unless
the compiler can do what I said. Hence, on a list of 10,000 elements,
the code there will take 10,000 stack frames. The compiler is now smart
enough to see what you're doing and generate tail-recursive code from it.
--
Darren New / San Diego, CA, USA (PST)
"That's pretty. Where's that?"
"It's the Age of Channelwood."
"We should go there on vacation some time."
Post a reply to this message
|
|