2. Using Array SyntaxMost Yorick statements look like algebraic formulas. A variable name is a string like Var_1 -- upper or lower case characters (case matters), digits, or underscores in any combination except that the first character may not be a digit. Expressions consist of the usual arithmetic operations + - * /, with parentheses to indicate the order of operations (when that order is different than or unclear from the ordinary rules of precedence in algebra). Elementary mathematical functions such as exp(x), cos(x), or atan(x) look just like that. Usually, a Yorick variable is a parametric representation of a mathematical function. The variable is an array of numbers which are values of the function at a number of points; few points to represent the function coarsely, more for an accurate rendition. The parameters of the function are the indices into the array, which rarely make an explicit appearance in Yorick programs. Thus,
defines a variable theta consisting of 100 evenly spaced values starting with 0.0 and ending with 2*pi. Now that theta has been defined as a list of 100 numbers, any function of theta has a concrete representation as a list of 100 numbers -- namely the values of the function at the 100 particular values of theta. Hence, variables x and y representing coordinates of the unit circle are defined with:
Here, cos and sin are built-in Yorick functions. Like most Yorick functions, they operate on an entire array of numbers, returning an array of like shape. Hence both x and y are now lists of 100 numbers -- the cosines and sines of the 100 numbers theta. The semicolon marks the end of a Yorick statement, allowing several statements to share a single line. The end of a line (i.e.- a newline) can also mark the end of a Yorick statement. However, if any parentheses are open, or if a binary operator or a comma is the last token on the line, then the newline is treated like a space or a tab character and does not terminate the Yorick statement. If a line ends with backslash, the following newline will never terminate the Yorick statement. (That is, backslash is the continuation character in Yorick.) I recommend that you never use a backslash -- end the line to be continued with a binary operator, or leave the comma separating subroutine arguments at the end of the line, or split a parenthetic expression across the line, and it will be continued automatically.
|