Sashan Govender | 12 Nov 14:41

Are pure functions cached

Hi

Is this sort of thing optimized in ocaml? I.e. the result of the
function cached implicitly? Assume that the function
'computationally_expensive_function' is pure (has no side-effects).

let computationally_expensive_function constant = (* something that
takes a while *)

computationally_expensive_function c
.
.
.
if computationally_expensive_function c then begin (* do something *) end

Whereas in C it's not true that the result is cached and you'd
probably rewrite the above as:

store = computationally_expensive_function(c);
.
.
.
if (store) { /* do something */ }

to prevent re-execution of the function.

Thanks

--
sashan
http://sashang.orcon.net.nz/

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Ads on Yahoo!

Learn more now.

Reach customers

searching for you.

Yahoo! Groups

Going Green Zone

Save the planet.

Your resources to go green.

.

__,_._,___
Jon Harrop | 12 Nov 16:01
Favicon

Re: Are pure functions cached

On Wednesday 12 November 2008 13:43:18 Sashan Govender wrote:
> Hi
>
> Is this sort of thing optimized in ocaml?

That isn't an optimization and, no, it isn't done by the current OCaml
implementation.

> I.e. the result of the
> function cached implicitly? Assume that the function
> 'computationally_expensive_function' is pure (has no side-effects).
>
> let computationally_expensive_function constant = (* something that
> takes a while *)
>
> computationally_expensive_function c
> .
> .
> .
> if computationally_expensive_function c then begin (* do something *) end
>
> Whereas in C it's not true that the result is cached and you'd
> probably rewrite the above as:
>
> store = computationally_expensive_function(c);
> .
> .
> .
> if (store) { /* do something */ }
>
> to prevent re-execution of the function.

You can use lazy or write a memoize function to achieve that in OCaml.

--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

New web site?

Drive traffic now.

Get your business

on Yahoo! search.

Health Groups

for people over 40

Join people who are

staying in shape.

.

__,_._,___
Dario Teixeira | 12 Nov 14:59
Favicon

Re: Are pure functions cached

Hi,

> Is this sort of thing optimized in ocaml? I.e. the result
> of the function cached implicitly? Assume that the function
> 'computationally_expensive_function' is pure (has
> no side-effects).

Nope. The technique you are referring to is called "memoization",
and can easily be added by a simple Camlp4 syntax extension:

http://en.wikipedia.org/wiki/Memoization
http://martin.jambon.free.fr/309/pa_memo.ml.html

Cheers,
Dario Teixeira

__._,_.___
Recent Activity
Visit Your Group
Yahoo! Finance

It's Now Personal

Guides, news,

advice & more.

Yahoo! Groups

Dog Group

Connect and share with

dog owners like you

Yahoo! Groups

Going Green Zone

Learn to go green.

Save energy. Save the planet.

.

__,_._,___

Gmane