Functions: Using Patterns in Delayed Assignment (:=)

Defining Functions with Patterns

Defining functions with patterns probably combines the most useful aspects of Mathematica.  Define a function that takes patten matching x as its first argument and an argument matching n as its second argument and returns x to the "index_104.gif" power:

"index_105.gif"

"index_106.gif"

"index_107.gif"

"index_108.gif"

"index_109.gif"

This works fine, but suppose we had defined  x ahead of time

"index_110.gif"

"index_111.gif"

"index_112.gif"

RHS evaluated, it is x^(second argument) which is currently 4^(second argument)

"index_113.gif"

"index_114.gif"

"index_115.gif"

"index_116.gif"

Delayed Assignmet (:=)

The problem with the above functions is that the evaluation is made immediately.  For a function, the argument serves as a placeholder for a future calculation.  You do not wish to have the right–hand–side evaluated until the actual values of left–hand–side are specified.  The remedy is to use delayed assignment :=

"index_117.gif"

"index_118.gif"

"index_119.gif"

"index_120.gif"

"index_121.gif"

"index_122.gif"

"index_123.gif"

"index_124.gif"

"index_125.gif"

"index_126.gif"

"index_127.gif"

"index_128.gif"

"index_129.gif"


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