Smarter Functions: Further Restriction on Patterns and Conditions

Restrictions on Patterns

The factorial function is pretty good, but not foolproof as the next few lines will show.

"index_152.gif"

"index_153.gif"

The next line will cause an error to appear on the message screen.

"index_154.gif"

"index_155.gif"

"index_156.gif"

"index_157.gif"

"index_158.gif"

"index_159.gif"

The remedy is to restrict the pattern:

"index_160.gif"

"index_161.gif"

This time it doesn' t produce an error, and returns a value indicating that it is leaving the function in symbolic form for values it doesn' t know about.

"index_162.gif"

"index_163.gif"

Functions and Patterns with Tests

However, the definition of factorial still needs some improvement--the next line will cause an error.

"index_164.gif"

"index_165.gif"

"index_166.gif"

"index_167.gif"

"index_168.gif"

"index_169.gif"

"index_170.gif"

"index_171.gif"

"index_172.gif"


Created by Wolfram Mathematica 6.0  (06 September 2007) Valid XHTML 1.1!