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.



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



The remedy is to restrict the pattern:



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.



Functions and Patterns with Tests

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









Spikey Created with Wolfram Mathematica 6