Next: , Previous: , Up: Operational Features   [Contents][Index]

3.11 Memoized Expressions

SCM memoizes the address of each occurence of an identifier’s value when first encountering it in a source expression. Subsequent executions of that memoized expression is faster because the memoized reference encodes where in the top-level or local environment its value is.

When procedures are displayed, the memoized locations appear in a format different from references which have not yet been executed. I find this a convenient aid to locating bugs and untested expressions.

For instance, open-input-file is defined as follows in Init5f4.scm:

(define (open-input-file str)
  (or (open-file str open_read)
      (and (procedure? could-not-open) (could-not-open) #f)
      (error "OPEN-INPUT-FILE couldn't open file " str)))

If open-input-file has not yet been used, the displayed procedure is similar to the original definition (lines wrapped for readability):

open-input-file ⇒
#<CLOSURE (str) (or (open-file str open_read)
 (and (procedure? could-not-open) (could-not-open) #f)
 (error "OPEN-INPUT-FILE couldn't open file " str))>

If we open a file using open-input-file, the sections of code used become memoized:

(open-input-file "r4rstest.scm") ⇒ #<input-port 3>
open-input-file ⇒
#<CLOSURE (str) (#@or (#@open-file #@0+0 #@open_read)
 (and (procedure? could-not-open) (could-not-open) #f)
 (error "OPEN-INPUT-FILE couldn't open file " str))>

If we cause open-input-file to execute other sections of code, they too become memoized:

(open-input-file "foo.scm") ⇒

ERROR: No such file or directory
ERROR: OPEN-INPUT-FILE couldn't open file  "foo.scm"

open-input-file ⇒
#<CLOSURE (str) (#@or (#@open-file #@0+0 #@open_read)
 (#@and (#@procedure? #@could-not-open) (could-not-open) #f)
 (#@error "OPEN-INPUT-FILE couldn't open file " #@0+0))>

Next: , Previous: , Up: Operational Features   [Contents][Index]