Language::Farnsworth is a programming language originally inspired by Frink (see http://futureboy.homeip.net/frinkdocs/ ).
However due to certain difficulties during the creation of it,
the syntax has changed slightly and the capabilities are also different.
Some things Language::Farnsworth can do a little better than Frink,
other areas Language::Farnsworth lacks (sometimes greatly).

Language::Farnsworth currently only supports a few escapes, this will be rectified in future versions of Language::Farnsworth but was not a priority for the early releases which are intended to just be not much more than a proof of concept

\" # to escape a quote inside a string
\\ # to escape a backslash inside a string

Language::Farnsworth also supports simple expressions to be interpolated inside of a string itself, the syntax is very similar to variable interpolation and can be used to interpolate a variable when you don't want to have some space around it.

Arrays in Language::Farnsworth are pretty simple to understand and create

[element, element, element, ...]

You can have any number of elements and they can contain anything that you can store in a variable (currently the only thing you cannot store in a variable is a function, but there is a solution to that, see below about Lambdas)

NOTE: This section and its syntax is VERY likely to change in future releases
NOTE: There is currently a known issue with push[] and arrays where one can cause it to keep from altering the original array, this will be fixed in future versions

You can access elements of arrays with syntax that looks like this

a = [1,2,3]
b = a@0$

You can also do an array slice by putting multiple elements in between the @ and $. For example:

Farnsworth has logical operators for dealing with boolean values, it has the standard ones || for OR, and && for AND, and !boolean for NOT. It also has one more additional one ^^ for XOR as I've found that to be useful in many situations

White space or parenthesis between things (numbers, variables, function calls, etc.) means that you want to implicitly multiply the two tokens

10 meters

is the same as

10 * meters

note that space around operators such as +-*/ does not imply multiplication, this means that if you wanted to multiply something by a negative number you MUST use a *, otherwise it will think you want to subtract

First we've got 'defun' this says that we're defining a function, the name is borrowed from lisp. Next we've got 'f' which is the name of the function. After that we've got any expression that evaluates to a lambda. You can read about lambdas in more detail below, but here's the basics; We start with {` this is the starting syntax of a lambda. Following it are the arguments to the lambda 'x' in this case, we then end the arguements with another '`'. Then we've got the body of the lambda which is just a series of statements that end up return[]ing a result.

Now lets have a look at a slightly more complicated function

defun max = {`x,y` var z; if (x > y) {z = x} else {z = y}; z}

here we've got a function 'max' that takes two arguments, 'x' and 'y', then we've got something new on the right side, 'var z; if (x > y) {z = x} else {z = y}; z', if you've programmed before you'll notice that we're separating each expression with a ';'

the very last expression that gets evaluated, in this case, 'z' is what the function returns if there isn't an explicit call to return[]

NOTE: That while this syntax is depreciated it is unlikely to be removed very soon as it does not get in the way of anything in the parser

To define a function you'll want to do something like this

f{x} := x+x

First we've got 'f' which is the name of the function, then we've got this weird little part following it '{x}' this defines the arguments that the functions takes, in this case its a single argument named 'x', next we've got ':=' this is the assignment operator for defining a function (it is also used for units, but we'll cover that later) then we've got the expression 'x+x' which is what the function actually does, in this case we're adding the argument to the function to itself

Now lets have a look at a slightly more complicated function

max{x,y} := { var z; if (x > y) {z = x} else {z = y}; z}

here we've got a function 'max' that takes two arguments, 'x' and 'y', then we've got something new on the right side, '{ var z; if (x > y) {z = x} else {z = y}; z}', we've surrounded the expression on the right with ' { } ', this lets us use multiple statements to build up the function if you've programmed before you'll realize that we're separating each expression with a ';'

the very last expression that gets evaluated, in this case, 'z' is what the function returns if there isn't an explicit call to return[]

After defining a function you really should be able to call them shouldn't you? there are two basic ways to call functions in Farnsworth

The simplest way is this

max[1,2]

this will call the function 'max' with the arguments '1'and '2'

There is also another way to call functions indirectly, this way shouldn't be used in most cases as it can be confused with unit conversions which we will cover later

[1,2] -> max
10 -> f

both of these methods call the functions to the right of '->' using the expressions on the left as arguments. This method should be used sparingly because it can be ambiguous and can actually cause problems when there is a unit the same name as a function that just happens to allow a proper conversion. In the standard library there is a unit f (short for femto) that will cause the following example to not work properly

f{x} := {x * x}
10 -> f

This will not in fact call the function f, but will end up telling you how many femtos will fit into 10. This is however a good way to do certain things that APPEAR to be a conversion between two things, but don't easily convert because there's some other factor involved. Temperature conversions between Celsius and Kelvin are possible this way.

C{x} := (x / K) - 273.15
10 K -> C

and you'll get back the result -263.15. That kind of conversion isn't possible to do with standard units as you'll begin to understand below.

Arguments to functions in Language::Farnsworth can have default parameters so that they don't always have to be specified explicitly. They are set when you create the function by setting the arguments equal to the default value

Currently type constraints have to be some expression that describes the type of input you are expecting, in this case we used "meter" however meter describes a length, and any expression that describes a length can be used as the argument to the function e.g.

f[10 feet]

is perfectly valid. There are plans to implement the ability to say something like 'f{x isa length} however they have not been implemented yet. You can combine default arguments and constraints by specifying the default argument first, e.g.

Sometimes you want to be able to take any number of arguments in order to perform some action on many different things, this is possible in Language::Farnsworth. You can do this by adding a constraint to the last argument to the function.

From this example you can see that we use the type constraint '...'. What this does is tell Language::Farnsworth to take any additional arguments and place them into an array and pass that array as the variable y. Here's an example of what use this can be to do something like recreate the map function from perl.

What we've got here is the first argument sub must be a Lambda (see below for more information on them). And the second argument swallows up ALL of the other arguments to the function allowing you to take any number of them.

NOTE: the map actually used in farnsworth is slightly more complex to handle some other edge cases

With the new function changes all functions are now treated the same as lambdas. This lets us do some neat things. This new syntax with '&' is only temporary until I finish the namespace code and add a proper way for when you want to get the value of a function.

Since Farnsworth represents everything as a single unit it will always want to give you back your calculations in that base unit; This isn't always what you want. So you can tell it to convert between the units to get exactly what you are after.

1 foot + 12 inches

When doing that calculation you would most likely want your answer back in 'feet', however farnsworth gives you back something like

0.6096 m

Now what the heck is that? You wanted feet didn't you? This is what the '->' operator is for, it will make farnsworth tell you the result in any unit you wish, so lets try this again

1 foot + 12 inches -> feet

and Farnsworth gives you back the single number '2'. That's the correct answer, but what if you wanted it to tell you '2 feet' instead? you can do this by putting the unit you want the result in in quotes that will tell Farnsworth that you want the answer to contain the unit also. So lets do this one more time

Farnsworth also supports the SI standard prefixes such as kilo, centi, nano, etc.

It however supports them on ALL units, so you can in fact say '1 kilosmoot' to mean 1000 smoots.

you can also define your own prefixes by doing this

kibi :- 1024
mibi :- 1024 * 1024

This allows you to add any prefixes you need to make a calculation simple and easy to do

NOTE: bits and bytes use the SI units of 1000 for kilobit, megabit, etc. to get the normal meaning of 1024 instead, use the of prefixes such as kibibit, mebibyte, etc. see http://en.wikipedia.org/wiki/Binary_prefix for more information on them.

Farnsworth also has loops, they look exactly like they do in C or Perl or Java

while ( condition ) { statements to run while the condition is true }

This is currently the only kind of loop that exists in Farnsworth, however ALL types of loops can be made from this, which is an exercise currently outside the scope of this document The braces around the statements are necessary as they are in Perl and Java. As with ifs you also need to have a semi-colon after the braces when you want to begin the next statement.

NOTE: for loops are definitely going to be added, i just haven't gotten to them yet.

Lambdas are a very neat feature of the Language::Farnsworth language, they are best described as something very similar to a subroutine reference in perl. When you create a lambda it keeps the environment with it that it was defined in (as far as variables are concerned anyway). This allows you to do things like create static variables between calls

Note: if anyone can think of a better name for these feel free to contact me about it. Also Note: the syntax for them MIGHT change as i begin to learn how to rewrite the parser to be smarter and fix a number of problems i have with it

As you can see here, a lambda is actually stored inside a variable rather than a different namespace like functions are, this allows you to have a variable contain the lambda and use it only inside the scope it was defined in, this also allows for fun results when nesting lambdas

Calling a lambda is fairly simple, the syntax looks a lot like the syntax for calling functions or for using units

lambda[arguments]
lambda argument
lambda * argument

What's going on here is that you are multiplying the lambda by its argument, which is either a single value or an array. When you do this the lambda gets passed the other item as its argument(s). This lets lambdas act and look like normal functions while behaving as a variable at the same time. The only thing to watch out for here though is that if you do something like

var mylamb = {`x` x^2};
var b = mylamb[10];

It will first try to find a FUNCTION named mylamb before calling your variable. So if you've got a variable you're storing a lambda in named the same as an existing function you'd want to do something like

var b = mylamb [10];
var b = (mylamb)[10];
var b = mylamb*[10];

I am considering changing this since the lambda would be scoped but it will not be until i have a way to explicitly get/use the function that was already defined.

argument lambda

This order will also work, but should be used sparingly because it can be confusing.

Since i've mentioned it before and an example is necessary of what nesting a lambda really means

index = ({`` var count=0; {`` count = count + 1}} []);

What we've got here is a lambda call inside of an expression that returns a lambda. Since lambdas carry the scopes that they were defined in around with them the lambda that index contains has access to the variable count and since it was defined outside of the nested lambda it does not get reset between calls, allowing it to continue incrementing count over and over. And because count was declared in the first lambda it isn't available to anything outside of that scope, meaning that count cannot be altered by anything other than the lambda that index now contains.