Wednesday, September 1, 2010

Defining constants

To day in class I showed an example of constant definition in Racket (formerly Scheme)

Student's question: Can you change it later?

Answer: no, you define that identifier once and for all. Once chosen, you cannot change it. It's like a baptism.

Actually that's not quite true, in fact it's the reverse. If a child is baptized "David", then his name will be David forever. But it is possible to use the name "David" for other children.

In Racket when you write: (define myfavoriteconstant 45), you take a number: 45, and give it a name: "myfavoriteconstant". From now on "myfavoriteconstant" refers to 45; that cannot be changed. However, nothing prevents you from choosing other names as well: you may next write
(define ilikethisnametoo 45)

1 comment:

Note: Only a member of this blog may post a comment.