[clean-list] Re: Question on destructive array updates

Attila Ondi aondi at fit.edu
Wed Oct 22 19:40:26 MEST 2008


Hi Carlos,

Yes, you are right, the array is created very time Fib5 is called. 
However, that was my concern since the function was called only once in my
small trial.  As you pointed out it is easy to change the function to
either use a global array or one passed to it (as in an environment).  I
was more interested in seeing how different techniques would work in a
functional language (particlularly in Clean) and how they relate t oeach
other in terms of performance.  You and Pieter helped me through my first
stumbling blocks :)

Thanks again,
Attila

> Hi Atilla,
>
> The point is that one needs to perform 'createArray' only once. Fib5_, and
> if I understood correctly, does that every time
>
> ...
> Fib5 :: Int -> Int
> Fib5 n
> | n >= 0 =
>     let
>         (res, _) = Memoize Fib5_ n (createArray (n+1) 0)
> more ...
>
> This is: for every 'n', you are saying 'createArray (n+1) 0'. So, you are
> not using memoization at all. And probably Memoize
> is calculating everything from scratch all the time...
>
> That's
> why I defined fibm = ... in the code at top level, so that way the
> definition itself, I hoped, captured this. I know that in Clean there
> are differences at the top level in the meaning of '='. Sometimes
> recalculates, sometimes is a macro (calculated only once). If I
> remember correctly, the way I did it is a macro definition and
> therefore calculated only once.
>
> cheers
> Carlos
>
>
> ----- Original Message ----
> Date: Tue, 21 Oct 2008 17:23:13 -0400 (EDT)
> From: "Attila Ondi" <aondi at fit.edu>
> Subject: Re: [clean-list] Question on destructive array updates
> To: clean-list at science.ru.nl
> Message-ID:
>     <3217.163.118.203.36.1224624193.squirrel at webaccess.fit.edu>
> Content-Type: text/plain;charset=iso-8859-1
>
> More questions :)
>
> I was playing around with Carlos's memoization implementation, especially
> after I realized it will store only the result of the topmost call (see f
> n in his memo function).  After a little fiddling, this is what I came up
> with to store all intermediate values:
>
> :: *ArrayTuple a :== (a, *{a})
>
> Memoize :: (Int -> ((Int -> (*{Int} -> ArrayTuple Int)) -> (*{Int} ->
> ArrayTuple Int))) Int *{Int} -> ArrayTuple Int
> Memoize f n a
> | size a < n+1 = abort ("Memoize: Array is smaller than required (size: "
> +++ (toString (size a)) +++ ", n: " +++ (toString n) +++ ")")
> | a.[n] < 0 = abort ("Memoize: Invalid array value (n: " +++ (toString n)
> +++ ", a[n]: " +++ (toString (a.[n])) +++ ")")
> %7



More information about the clean-list mailing list