Pros and Cons of PicoLisp Unfolded
Tags: picolisp, lisp, functional, programming
Published: 2021-10-10 08:35:06

This article will present you the **pros and cons** of the programming language PicoLisp. It will be only a rough overview - we will come back to all of these points in more detail later.

Secondly, we will take a look at a very small **PicoLisp web app** to get a first impression how the language looks like.

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

### Tractatus Pico Blaesicus

The article is inspired by the famous "[Tractatus Logico-Philosophicus](https://en.wikipedia.org/wiki/Tractatus_Logico-Philosophicus)" of the German philosopher Ludwig Wittgenstein. 

In the original Tractatus, each proposition is a kind of declarative statement that is further elaborated on sub-levels. If you are interested in philosophy, you might know the first few sentences:

> 1 The world is all that is the case.

> 1.1 The world is the totality of facts, not of things.

> 1.11 The world is determined by the facts, and by their being all the facts.

And so on and so forth. Very interesting, and very hard to read - but this just as a side note, because we want to talk about PicoLisp.

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

There is a little fun web app inspired by the Tractatus, which lists the pros and cons of PicoLisp: The [Tractatus Pico Blaesicus](https://picolisp.com/tractatus/). (The Latin word "blaesus" means "lisping" 😀) Let's take a look at it - first at the content, then at the sources.

*I recommend to open the [link](https://picolisp.com/tractatus/) in another browser tab while reading this article.*

![tractatus1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629125212962/g5WCqlHNL.png)

Similarly to the Tractatus, it contains items that unfold the sublevels.

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

### The Pros of PicoLisp

Let's start with the good news and click on "cons". PicoLisp is:
- Simple
- Transparent
- Powerful
- Efficient.

Well, anybody can say that, but what is meant by that? Let's open up the next level.

![tractatus2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629125408428/6003-Txgh.png)

There are still more sublevels which elaborate the statements further. You get the principle - feel free to check out explore the sublayers on your own.


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

### The Cons of PicoLisp

Nothing is perfect, here are the cons:
- Lisp syntax
- Interpreter-only
- Omitted features
- Flaws

Or, to be more precise:

![tractatus3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629127439411/7VTH-cg-L.png)

Again, feel free to explore the sublayers on your own. We will not go into detail about the content now, since each of these items is worth its own post.

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

## Analyzing the "Tractatus" Web App 

The Tractatus Pico-Blaesicus is also a nice example about the strength of PicoLisp: to write a dynamically rendering HTML page with extremely few lines of code.

Let's download the code and look at it with the eyes of someone who doesn't know any PicoLisp at all.

### Download

To get the source code, type 
```
$ wget https://gitlab.com/picolisp-blog/single-plage-scripts/-/raw/main/tractatus/tractatus.l
```
or open [this link](https://gitlab.com/picolisp-blog/single-plage-scripts/-/blob/main/tractatus/tractatus.l) in the browser and download it manually.

### Running the script

In the folder where the script has downloaded, type

``` 
$ pil tractatus.l
```

and point your browser to http://localhost:8080. You should see the page rendering like this - it works!

![tractatus.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1629128193972/KCPsW9KrS.png)

(if you haven't installed PicoLisp yet, [read this first](https://picolisp-blog.hashnode.dev/how-to-install-picolisp)).

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

### A first impression

The second purpose of this article is to give an impression how PicoLisp looks like. If you are only used to Python or JavaScript, it might look very foreign to you at first.

So here is a challenge for you: **Try to take a look at the [source code](https://gitlab.com/picolisp-blog/single-plage-scripts/-/blob/main/tractatus/tractatus.l) and figure out roughly what is going on! (no details!)**


### Finding 1: The ``level`` function

Let's start with the most obvious: starting from line 67, we find the actual text.

```
# Text
Pro
   Simple
      Simple NOT in the sense of "small number of features"
               ...
```

Since the numbering is missing, there must be some algorithm that "counts" the spaces and converts it to numbering.  We can guess that this is done using the ``level`` function, where we find the term ``sp?`` which could maybe represent the "space" characters: 

```
# Content
(de level (Lst)
   (for (I . C) Lst
      (NIL (sp? C) I) ) )
```


### Finding 2: The ``menu`` Function

The``level``-function gets called  within a certain ``menu``-function:

```
 [<menu>
     ~(let (L (0)  L1 (prog (skip "#") (line))  L2 (line))
        (recur (L)
           ...
              (let I (level L1)
                 (loop
                     ...
```

We see two variables, ``L1`` and ``L2`` which might be  two consecutive lines. The program **loops** over ``L1`` and ``L2`` and calls the ``level`` function on them. And we also know the result: there will be clickable items that can expand and show subitems. So this reminds us... of a **navigation menu**, right?  

**The code transforms the text items to a navigation menu, which are then rendered to an HTML page.**


### Finding 3: Serving HTML and CSS

If you did some web programming before, you know that the browser usually expect a HTML and also a CSS file. However, we only have one single page of PicoLisp...?! 

This is because PicoLisp creates everything from the source code. The browser "thinks" that it gets a css file, but in reality it is only a function.

```
# CSS
(de tractatus.css ()
   (httpHead "text/css" 86400 "!tractatus.css")
   (ht:Out *Chunked
      (prinl "html {background-color: #eee}")
      (prinl "body {margin: auto; max-width: 96ex; border: 1px solid #bbb; font: 20px serif; background-color: #ffd; padding: 2em 5% 4em 5%}")
   ...
```

In the end, everything get mapped to a HTML text stream that is displayed in the browser using the function ``httpEcho``:

```
## Source
(de tractatus.l ()
   (httpEcho `(pack (car (file)) (cadr (file))) "text/octet-stream" 3600 T) )
```

### Finding 4: The Server

If there is a browser **client**, then there must also be a **server**. And right, at the very end of the script, we can also find the server, listening on port 8080:

```
(server (or (format (sys "PORT")) 8080) "!tractatus")
```

Now we also know why we can access the script through ``localhost:8080``.

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

### Summary

With this, we got a first impression of the pros and cons on a very rough level. We will keep it in mind for further posts. 

Also, we have seen our first PicoLisp script: Without going too much in detail, we see that we can create the client, server and content in just a few lines of code.

If you are interested to understand how this works exactly, stay tuned for the ["Getting Started"](https://picolisp-blog.hashnode.dev/series/getting-started) and ["Web Application Basics"](https://picolisp-blog.hashnode.dev/series/web-app)-Tutorials. Of course, you can also check out the documentation and tutorials on the [PicoLisp](http://https://picolisp.com)-site by yourself.

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

## Sources

- https://picolisp.com/tractatus/!tractatus  
- https://en.wikipedia.org/wiki/Tractatus_Logico-Philosophicus