60 PicoLisp Functions You Should Know - 2: Defining Variables
Tags: picolisp, lisp, functional, programming
Published: 2021-10-16 10:31:25

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](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop). If you want to follow along, type ``pil +`` in the console to open the REPL. Check [here](https://picolisp-blog.hashnode.dev/60-picolisp-functions-you-should-know-1-arithmetics) for further basic commands.*

-------------------

### Defining Variables

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](https://picolisp-blog.hashnode.dev/concepts-and-data-types), 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](https://en.wikipedia.org/wiki/Anonymous_function)) 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"
```

----------------------------

### Internal representation

In order to fully understand what is happening, let's apply what we learned in the [Concept and Data types](https://picolisp-blog.hashnode.dev/concepts-and-data-types) post. This is the internal representation of symbol ``Japan``:

![keyval.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1630855321522/2pZYEoQMN.png)

*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 ``VAL``cell, 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!

----------------------------

### Sources
- Cover pic: <a href="https://pixabay.com/de/users/geralt-9301/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1453836">Gerd Altmann</a> on <a href="https://pixabay.com/de/?utm_source=link-attribution&amp;utm_medium=referral&amp;utm_campaign=image&amp;utm_content=1453836">Pixabay</a>  
- https://software-lab.de/doc/index.html  
- https://software-lab.de/doc/tut.html#fun  
- https://picolisp.com/wiki/?RosettaCodeAnalysis