variables in fig

this is part 1 of an introductory series on fig programming. for the introduction to the series, go here. for the next part in the series, go here.


part 1: variables in fig


to fully appreciate the power and feel of coding, you must write or at least “run” code. you can get a glimpse of this from watching someone else run code, but you will not experience the confidence or joy of being able to tell the computer something and have it do what you say unless you actually do it.

one of the goals of fig is to avoid “formulas” by default. in fact you can create all kinds of formulas using fig, and if it feels too limiting you can add python code inline to a fig program. python allows full formulaic expressions if you want them.

by allowing this kind of extension, fig is both friendly and powerful. in fact you could simply start with python if you had the interest in doing so, but fig will provide a number of friendly shortcuts that allow you to quickly progress without getting slowed down by too many implementation details.

variables are one of the most universal aspects of programming, and fig requires you to either use python or at least one variable to create a “Hello, world” program.

a few keywords in fig are designed to stand alone, but in general most lines of fig code begins with a name supplied by the user. you can use a letter, like z or the word Now (fig is not case-sensitive, so Now and now and NOW refer to the same variable) but this “main variable” is a feature of fig that often saves you the trouble of referring to it several times in the same line.

fig is designed to be read left-to-right, and does not use parentheses… at least they do not have importance in syntax; they’re decorative, and strictly for the user to decide where they’re needed. in fig, our line begins with a variable. for the moment, let’s just use “now”:

now "Hello, world!"

in basic, this would be now$ = "Hello, world!" and in python it would be the same, except without the $ after the word “now”.

in fig this creates (or resets) a variable named now — which also gets set to a numeric value of zero (0.)

the next part of the line changes the value from 0 to a “string” of characters in quotes, in this case the phrase "Hello, world!" so far, nothing visible is done with this information. the program sets the value of a single variable, and does nothing else. but it goes left-to-right very clearly:

  • the first bit of text names and creates a variable.
  • the second bit of text changes the information (the value) that is stored by the variable.
  • the next thing we add to the line will probably do something with that variable, too.
now "Hello, world!"  print

okay, so now we’ve added the print command. so our variable now is output (“printed”) to the screen.

you could do other things with the now variable; you could make its contents all upper-case, before or after it prints, using the ucasecommand:

now "Hello, world!"  print  ucase

this changes the value to upper case, but (going left to right) it prints before that happens, so no change is yet visible. this:

now "Hello, world!"  ucase  print

…changes the value before using print so you can see the change in the output. order matters; and the order starts at the left and goes straight onward.

fig is different than many languages in that the most obvious or typical example (Hello, world) is usually made purely of output statements likeprint. but in fig, you begin with variables right away.

this is primarily done for friendliness, but it also means that your first program will likely contain 2 of the 7 core concepts, (variables and output) instead of just output.

although this is functionally similar to a formula, i think it’s a better introduction to what programming is really about. fig doesnt actually avoid the functionality of a formula, but the parenthetical way of doing one that makes it “too much like math” to the math-phobic. it is still mathematical in nature, but closer to natural or spoken language in structure.

copying variables is much like setting them:

if you want to copy the value of the variable now into say, another:

now "Hello, world!" ucase
another now

now still contains the string "Hello, world!" but so does another, and you can use/change each variable independently. you could also use one variable as a backup, to compare the two later to see if the other has changed.

each of the two lines above start with a “main variable” and the program code on each line works with the variable on the far left:

  • putting a value after the main variable (this is optional) will set it.
  • putting another variable (also optional) after the main variable will copy its value into the main variable.
  • some commands will read or use, but not change the main variable; print is an example.
  • some commands will write or change the main variable, but not read it.
  • some commands will read and change the main variable– ucase is such an example.
  • some commands such as cls, will not read or change the main variable. cls simply clears the screen.

most commands in fig share a line / begin a line with a main variable, which is not optional. when a new version of fig gains a feature, it is generally a “shared-line” command, or not a program keyword at all. (for example, the help system in fig is not a keyword in the fig programming language, but it is a feature.)

a few keywords do not share a line; they get their own line and do not require (nor use) a main variable; commands/keywords of this type are very rarely added to fig.

these “own-line” commands are:

graphics / textmode

for / forin


fig / next / nextin / wend

iftrue / ifequal / ifmore / ifless / else

try / except / resume

these commands are so important, they get their own line– if only to stand out as part of a command block (a multi-line series of commands) or as a command that significantly changes the behavior or mode of the program.

all other commands require a line that starts with a main variable:

now  cls  # cls is a command, now is main var

in the above example, # is a special case– it tells fig to ignore the rest of the line so that it can be used to “comment” the program. technically# is a command or keyword, but it can be on a shared-line with a main variable, or begin a line, or be on a line by itself.

however, it also isnt really part of the program; it just begins a note inside the program code.

if you put a # in front of a line of working code, it will turn that line into a comment and thus disable the line or stop it from running as part of the program. this can be useful for testing the functionality of a single line or group of lines, and using it this way is called “commenting out” a line.

what else can be said about variables? so far, we have only demonstrated keywords that work without any additional details in the way they are written: most keywords, like cls and print and ucase are simply tacked on to the rest of a line, at the right of the line. the program goes from left to right, and then to the next line.

as mentioned, there are keywords which go on their own line, such as textmode or while, and do not share a line with a “main variable” to start, or with other commands. but we haven’t talked about parameters yet.

apart from the start of a line, or the second “word” in a line (where it is used to copy one variable’s value into another) the place you are most likely to see a variable is as the parameter for a command that has them.

most fig commands have zero parameters, or one parameter. a few have more than one, but each command has the same (fixed) number of parameters in every context, except for function (used to define new fig commands.)

therefore cls always has zero parameters; while always has zero parameters; ucase always has zero parameters, and textmode always has zero parameters.

the keywords left and right always have a single parameter; it defines how many characters you want to keep from one side of a string.

to demonstrate:

now "hello"    print    left 2    print 

first now is created, set to "hello", then printed, then left 2 is run before the result is printed again. the output is:


we’ve used four spaces to separate each command, though one space is sufficient.

the left command includes 2 as a parameter, as the number of characters to take from the side of the string that’s stored in the main variable. it then changes the main variable to the leftmost 2 characters.

since print is called before and after left 2 runs, it displays the value of now before and after as well.

like most commands that have parameters, left does not have to use a “constant” value like 2, but can use a variable instead:

howmany 2
now "hello"    print    left howmany    print

using variables as parameters allows the program to have more control at run-time (it allows it to be more flexible and powerful) and is somewhat the essence of things being “programmable.” it allows the program to take input and have it affect the output, even before we get into conditionals.

as mentioned, the extra spaces are optional command separators (purely for the visual aspect of the code) and each of these lines are valid and do the same thing:

now "hello"    print    left howmany    print
now "hello" print left howmany print
now "hello" : print : left howmany : print
now "hello" ; print ; left howmany ; print

the colon : is the traditional command separator in basic, and in c and python and javascript, a semicolon ; is used.

it is worth talking about variable types (or rather data types, since any variable in fig could hold one or more type of value) but the subject is simple enough that the point of mentioning it might not be obvious:

  • string type: data “in quotes” is a string of characters, which may include numbers but they will not be treated as a numeric value."5" plus "5" returns "55"
  • integer type: an integer, also known as a “whole number,” is a numeric and has no decimal point. 5 plus 5 returns 10
  • float type: numeric and includes a decimal point; allows decimal values. like basic, fig handles floats and integers pretty seamlessly, but strings have to be converted using the val command to be treated as numerics. 5 plus 5.7 returns 10.7
  • array type: while variables hold one value at a time, a variable can be converted to an array holding the same value, plus others.

now 5 arr # create an array holding 5

now 5 arr times 100 # array holds 100 5s

normally the times command is used for basic multiplication, but when used on a string it sticks several copies of that string together into one:

p "hello" times 4 # "hellohellohellohello" 

when you start a line with a main variable that you’ve already used, it sets it to zero:

p  print          # displays 0
p  5  print       # displays 5
p  print          # displays 0 again
p  plus 7  print  # displays 7

arrays are the exception to this rule. since they might hold a lot of information, they do not get cleared on use as a main variable unless you explicitly clear them:

p 5  arr print    # displays [5]
p  print          # displays [5]
p 0  print        # displays 0

5 plus "5" plus 5.0 returns [5, '5', 5.0]

arrays in fig are the same as arrays in python, known in python as “lists.” lists in python (like in fig) are not restricted to a single type per array; they can hold a mix.

apart from converting a variable to array using arr, fig has several ways of creating an array:

  • the split command can split a string into array elements.
  • arropen and arrcurl load the lines of a file or webpage into an array, respectively.
  • arrstdin creates an array from information “piped” in from another program. this allows you to mix the functionality of various programs together, even if they aren’t designed as a group or suite.
  • command creates an array of parameters the fig program was called with. these are not parameters of individual commands, but of the program itself. (this allows you to put together your own “language” from fig programs and others, such as bash utilities.)
  • inline python can be used to create arrays.
  • arrshell runs command line programs, and returns the output of those programs as an array.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s