Next / Previous / Contents / Shipman's homepage

5.7. The “let” convention

Sometimes we find that the same phrasing occurs at several positions within an intended function. To reduce repetition, and also to bring out the fact that we are talking about the same value, you may wish to define a sort of temporary shorthand name for a phrase in a construct with this general form:

  # [ let
  #     name1 = phrase1
  #     name2 = phrase2
  #   in ->
  #     simple or compound intended function ]

For example, suppose a prime appends the contents of one named file to another, and their names in the code are inFileName and outFileName. Here is its intended function:

  # [ file named by outFileName  :=  (file named by outFileName) +
  #     (contents of file named by inFileName) ]

Now suppose we complicate the design by stating that the input file is the one named by inFileName, but defaulting to standard input if inFileName is None; and the output file is either the one named by outFileName, or the standard output stream if outFileName is None. The intended function has now become quite unwieldy.

Using the let convention, we can write it by defining two shorthand names for the effective input and output files.

  # [ let
  #     in-file == the file named by inFileName, defaulting to sys.stdin
  #                if inFileName is None
  #     out-file == the file named by outFileName, defaulting to sys.stdout
  #                 if outFileName is None
  #   in ->
  #     out-file  :=  out-file + (contents of in-file) ]