lundi 6 avril 2020

Stream of random: When does evaluation happen?

The following code, I thought, should define a stream of random numbers between 1 and 10:

(define random-stream (stream-cons (random 1 11) random-stream))

However, what it actually does is define a stream of a specific random number. For example:

> (stream->list (stream-take random-stream 10))
'(5 5 5 5 5 5 5 5 5 5)

I presume this is the random number that (random 1 11) produces when the definition is first parsed. I got around this by making random-stream an argument-less function:

(define (random-stream) (stream-cons (random 1 11) (random-stream)))

This works:

> (stream->list (stream-take (random-stream) 10))
'(6 1 10 9 4 2 2 3 3 10)

So it looks to me that constants are, understandably, evaluated at read time, whereas functions are evaluated at call time. Usually this wouldn't matter, but in the case of a stream -- where you've got a recursive definition -- this makes a difference.

Is this how it works, or is it more subtle than this? Are there other cases one should be aware of regarding this difference?




Aucun commentaire:

Enregistrer un commentaire