Laziness, automatic memoization and arrays

Hi, I have a very simple question about laziness, memoization and arrays. If I have: import Data.Array a = listArray (1,10) $ map (\x -> (x*x)) [1..10] main = do print c ! 4 print c ! 4 print c ! 4 Can we assume that the operation 4 * 4 is evaluated only once, when first calling print c ! 4 in the main method? Oscar

On Sat, Aug 27, 2011 at 07:03:00PM -0400, Oscar Picasso wrote:
Hi,
I have a very simple question about laziness, memoization and arrays. If I have:
import Data.Array
a = listArray (1,10) $ map (\x -> (x*x)) [1..10]
main = do print c ! 4 print c ! 4 print c ! 4
Can we assume that the operation 4 * 4 is evaluated only once, when first calling print c ! 4 in the main method?
Yes. Any value with a name (such as c in your example) will only be evaluated once, no matter how many times you refer to it. -Brent

On Sunday 28 August 2011, 01:03:00, Oscar Picasso wrote:
Hi,
I have a very simple question about laziness, memoization and arrays. If I have:
import Data.Array
a = listArray (1,10) $ map (\x -> (x*x)) [1..10]
main = do print c ! 4 print c ! 4 print c ! 4
Needs parentheses, print (c ! 4) Without, it parses `(print c) ! 4' since function application binds tighter than infix operators.
Can we assume that the operation 4 * 4 is evaluated only once, when first calling print c ! 4 in the main method?
Yes. Although technically an implementation is allowed to recompute the value, no serious implementation will.
participants (3)
-
Brent Yorgey
-
Daniel Fischer
-
Oscar Picasso