60 PicoLisp Functions You Should Know - 2: Defining Variables
"One man's constant is another man's variable" (Alan Perlis)

Search for a command to run...
"One man's constant is another man's variable" (Alan Perlis)

No comments yet. Be the first to comment.
This is for users who want to start with PicoLisp from scratch. This series will cover installation, basic concepts, syntax, how to use the REPL, create simple scripts, read the docs and debug.
Hello, World!
In our previous post, we introduced how to utilize Discrete Event Simulation (DES) in PicoLisp. Now, let's explore another application: a railroad model simulation that includes tracks and trains. The visualization is ASCII-based, so feel free to unl...

The Dining Philosophers

In this post, we will introduce the concept of co-routines and how they are handled in PicoLisp. This will be the foundation for the subsequent posts on Discrete Event Simulation and finally a couple of examples, including an ASCII model railway simu...

One of the many nice things of PicoLisp is that it comes with a built-in database. This makes it easy to collect and store our own data, for example via a mobile app. But not all data is best represented in tables and lists: sometimes a colorful litt...

In the last two posts, we have built a little app that shows the current location and displays the nearest ice cream shops: https://picolisp-explored.com/an-openstreetmap-app-written-in-picolisp-pt-1 https://picolisp-explored.com/an-openstreetmap-ap...

On this page
Welcome back to the second part of the "60 PicoLisp Functions you should know" series.
This time, we will discuss about variables: How to define them, how to work with them, and how to understand their internal representation.
All examples can be executed in the PicoLisp REPL. If you want to follow along, type pil + in the console to open the REPL. Check here for further basic commands.
Variables can be set by the command setq.
: (setq A 3)
-> 3
The value of a symbol can be checked in the REPL like this:
: A
-> 3
We see that A evaluates to 3. The value of an undefined symbol is NIL.
: B
-> NIL
Now let's try the same with more complex symbols. Let's set a symbol Country to Japan. Let's try it the naive way (spoiler: it won't work):
: (setq Country Japan)
-> NIL
: Country
-> NIL
Country is NIL - what happened? As we learned in the previous section, basically everything is a symbol, except for lists and numbers. For the interpreter, Japan is a symbol and can be evalulated. However, Japan doesn't have a value, therefore the evaluation returns NIL, and Country is set to NIL, too.
Now let's set the Country to the symbol Japan itself. For this we need escape the evaluation by using a single quote ('). Then it will take the symbol Japan "as such":
: (setq Country 'Japan)
-> Japan
: Country
-> Japan
: Japan
-> NIL
This is an important difference.
The value of a symbol can be returned by val:
: (val 'Country)
-> Japan
: (val 'Japan)
-> NIL
We will come back to the quote(') function again when we will cover anonymous functions (lambda calculus) and other concepts of functional programming.
Any variable can have properties (key-value pairs), which are defined by put. Now let's fill up our symbol Japan with some key-value pairs, and give it a value "JP".
: (put 'Japan 'language "Japanese")
-> Japanese
: (put 'Japan 'continent "Asia")
-> Asia
: (put 'Japan 'island T)
-> T
: (setq Japan "JP")
-> "JP"
To check the value of a property, we will use get.
: (get 'Japan 'language)
-> "Japanese"
To print the variable with all it's properties, we can use the command show.
: (show 'Japan)
Japan "JP"
language "Japanese"
island
continent "Asia"
In order to fully understand what is happening, let's apply what we learned in the Concept and Data types post. This is the internal representation of symbol Japan:

Note that the key "island" does not need a value cell for T, because its existence already implies that it's T.
Now we can see the difference between Japan and 'Japan:
Japan evaluates to the content of the VALcell, which is "JP". 'Japan returns the pointer to the symbol Japan, i.e. the "symbol" arrow that we see at the top.Understanding this difference will be very helpful when we discuss more complex operations, for example on lists.
Congratulations, now you know can do basic arithmetics and define variables in PicoLisp. Let's continue with input/output and loops in the next post!
Cover pic: Gerd Altmann on Pixabay
https://software-lab.de/doc/index.html
https://software-lab.de/doc/tut.html#fun
https://picolisp.com/wiki/?RosettaCodeAnalysis