Semipredicate Problem - Solutions

Solutions

The semipredicate problem is not universal among functions that can fail. If the function's range does not cover the entire data type defined for the function, a value known to be impossible under normal computation can be used. For example, consider the function index, which takes a string and a substring, and returns the integer index of the substring in the main string. If the search fails, the function may be programmed to return -1 (or any other negative value), since this can never signify a successful result.

This solution has its problems, though; it overloads the natural meaning of a function with an arbitrary convention. First, the programmer must remember specific failure values for many functions, which of course cannot be identical if the functions have different domains. Second, a different implementation of the same function may choose to use a different failure value, resulting in possible bugs when programmers move from environment to environment. Third, if the failing function wishes to communicate useful information about why it had failed, one failure value is insufficient. Fourth, a signed integer halves the possible index range to be able to store the sign bit.

Read more about this topic:  Semipredicate Problem

Famous quotes containing the word solutions:

    Football strategy does not originate in a scrimmage: it is useless to expect solutions in a political compaign.
    Walter Lippmann (1889–1974)

    Those great ideas which come to you in your sleep just before you awake in morning, those solutions to the world’s problems which, in the light of day, turn out to be duds of the puniest order, couldn’t they be put to some use, after all?
    Robert Benchley (1889–1945)

    Science fiction writers foresee the inevitable, and although problems and catastrophes may be inevitable, solutions are not.
    Isaac Asimov (1920–1992)