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_158.gif"

"index_159.gif"

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

"index_160.gif"

"index_161.gif"

The remedy is to restrict the pattern:

"index_162.gif"

"index_163.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_164.gif"

"index_165.gif"

Functions and Patterns with Tests

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

"index_166.gif"

"index_167.gif"

"index_168.gif"

"index_169.gif"

"index_170.gif"

"index_171.gif"

"index_172.gif"

"index_173.gif"

Spikey Created with Wolfram Mathematica 6