In the following the examples, the **=⇒** symbols denotes output received from
Taffy, whether through assignment result, or IO output. The ellipses **…**
denotes continued output that is elided.

Table of Contents:

## Hello, Taffy, with a Class

In this example we create a class with a single method: sayHello.

class Fido { (@) sayHello { putLine("Hello, Taffy!") } } doggy = new Fido doggy sayHello ==> Hello, Taffy!

What if we want to say hello to a specific person? We then want to use an argument to the function sayHello. We do this by appending a colon to it:

sayHello: name

Here’s the full example:

class Fido { (@) sayHello: name { putLine("Hello, #[name]!") } } doggy = new Fido doggy sayHello: "Odif" ==> Hello, Odif!

**Remember:** Arguments are passed by reference unless their class is declared
atomic.

## Fibonacci Sequence

The following prints out the first ten fibonacci sequence numbers:

// define the function f(n) = f(n - 1) + f(n - 2) // define the base cases f(1) = 1 f(0) = 0 for (a = 0; a < 10; a++) { putLine("fibonacci(#[a]) = #[f(a)]") } ==> fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(2) = 1 fibonacci(3) = 2 fibonacci(4) = 3 fibonacci(5) = 5 fibonacci(6) = 8 fibonacci(7) = 13 fibonacci(8) = 21 fibonacci(9) = 34

## Calculus

Use the **derive** and **integrate** functions to derive and integrate a Function, respectively:

f(x) = sin(x) f derive ==> #F(x) = cos(x) g(x) = f(x)^2 f derive ==> #F(x) = 2 * cos(x) * sin(x) f integrate ==> #F(x) = -cos(x) {x^2} derive ==> #F(x) = 2x {sin(x) * x} integrate ==> #F(x) = sin(x) - x * cos(x) // assign the result to a new function, and add 2 to it integrated = [{sin(x) * x} integrate] integrated + 2 ==> #F(x) = sin(x) - x * cos(x) + 2

## Plot a Circle

A unit circle is be defined by **x^2 + y^2 = 1**, or parametrically by two
functions: **x(t) = sin(t)** and **y(t) = cos(t)**. The following example defines
the **x** and **y** functions, and evaluates them from **0** to **2PI**:

x(t) = sin(t) y(t) = cos(t) for (a = 0; a <= 2PI; a += PI / 100) { io putLine: "#[x(a)], #[y(a)]" } ==> 0, 1 0.031410759078128, 0.99950656036573 0.062790519529313, 0.99802672842827 ... 1, 0 ... 0, -1 ...

## Rotation Matrix

A rotation matrix is defined as:

```
|cos(θ) -sin(θ)|
|sin(θ) cos(θ)|
```

We use a combination Function and Matrix classes to create a rotation matrix, and rotate the point {0, 1} by 45 degrees.

// define a rotation matrix -- a Function of a matrix rot(theta) = ||cos(theta), -sin(theta) ; sin(theta), cos(theta)|| // rotate the point {0, 1}, by 45 degrees (PI / 2) rot(PI / 2) * ||0 ; 1|| ==> ||-1 ; 0|| // we can also create a Function that defines the input vector for us f(x, y, theta) = rot(theta) * ||x ; y|| // rotate the point {1, 0} by 45 degrees f(1, 0, PI / 2) ==> ||0 ; 1||

## The Lazy Caterer’s Sequence

The lazy caterer’s sequence defines the maximum number of pizza slices that can be made with a given number of straight slices through the pizza.

The formula is expressed as:

```
2
s(n) = n + n + 2
-----------
2
```

or, by using binomial coefficients:

`s(n) = choose(n + 1, 2) + 1`

So we create a sequence and look at the first 20 iterations:

seq = [Sequence createWithBlock: {<n> choose(n + 1, 2) + 1}] io putLine: seq[1, 20] ==> [2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79, 92, 106, 121, 137, 154, 172, 191, 211]

## Calculate PI

We can calculate PI by using the Chudnovsky algorithm:

(taken from Wikipedia)

We define this series in Taffy by using a combination of the Function and Series classes. We also need a higher-than-normal Number digit limit, to account for the extra numeric precision needed.

// push a high digit limit for extra precision [Number pushDigitLimit: 300] // define a Series that contains the series component of the // Chudnovsky algorithm series = [Series createWithBlock: {<k> ((-1)^k * ((6k)! * (13591409 + 545140134k)) / ((3k)! * ((k)!)^3 * (640320)^(3k + 3/2)))}] // a Function completes the formation g(x) = 1 / (12 * series[0, x]) // evaluate the first 50 iterations of the series io putLine: [[g(50) asString] substringFrom: 0 to: 55] ==> 3.14159265358979323846264338327950288419716939937510582 // we're done with the high digit limit, pop to conserve memory [Number popDigitLimit]

## Complex Numbers

In this example we use the **putLine()** function to print mathematical results.
We find the square root of -4, and the result of (i + 2) / 2.

putLine("The square root of -4 is: #[sqrt(-4)]") putLine("(i + 2) / 2 is: #[(i + 2) / 2]") ==> The square root of -4 is: 2i (i + 2) / 2 is: (1 + 0.5i)

Here we assign the the result of sqrt(-4) to a variable, and then use that variable in a new operation.

a = sqrt(-4) a^4 ==> 16