Skip to content

tamgucalc (en)

Claude Roux edited this page Jun 22, 2020 · 32 revisions

A Spreadsheet in Character Mode With Lisp Formulas: TAMGUCALC

Version française

This extremely lightweight spreadsheet launches instantly into your terminal. It allows you to enter numbers, define labels, and most importantly, to enter numerical formulas in Lisp. You can then save your spreadsheets with their formulas in a CSV file. You can also export your worksheets, keeping only the raw data.

The code is less than 700 lines long and is entirely modifiable at your convenience, if you want to add your own options.

Initialization

By default, tamgucalc uses the dimensions of your terminal to define the number of rows and columns.

Column size

The columns have a default size of 10 characters. To change this size, you will have to modify the "columnsize" variable in the code. Note that your spreadsheets save this size as well as the number of rows and columns in its file.

Enter a value

To enter a value, simply position the cursor on a cell with the arrows and enter your value. You can then move to the next cell with the arrows or press the "enter" key. A cell can contain numbers, formulas or labels.

Enter a Lisp formula

To enter a formula, simply position on a cell and type: '('. When your formula is complete, the system checks that the parentheses balance and displays the message: "Ready to compute".

Just press: "enter" and it will be calculated.

Selecting a cell

When you type a formula, you can use the arrows to select the cells that go into your formula. When you have selected a cell, press "enter" to save it in your formula.

Selecting a cell range

To choose a cell range, you must first choose a first cell, then type ".." (two points) and then choose a second cell. tamgucalc then replaces your selection automatically with the following rules:

The choice of a cell appears in the following form:

  • mat[i:j]: a single cell at positions i,j.
  • mat[:c][a:b]: 'c' is the column number, 'a,b' are the rows from 'a' to 'b' in column 'c'.
  • mat[r:][a:b]: 'r' is the row number, 'a,b' are the columns from 'a' to 'b' in row 'r'.

Note, the use of "r:" and ":c" to identify rows and columns.

Important: Column and row selections are translated as Tamgu vectors, which explains that in formulas that translate your selections, 'b' will always be "selection + 1".

Multiple rows and columns

Note that when you define a domain that includes several rows and columns, tamgucalc introduces an "&&&" operator to merge all the selected rows into a single data vector. In this way, your selection becomes a single element.

Example:

(sum (&&& mat[1:][1:3] mat[2:][1:3])

Formulas

Formulas in tamgucalc must be written in the Lisp defined for Tamgu (see documentation). You can define lambdas functions, functions (defun) or simply use the basic operators.

  • By default, all numerical values are "double" in the C direction (float type for tamgu).

Operators

Traditional operators such as: +,-,/, * are of course available.

You can also use: "sum" and "product". Note that "+" is used for atomic values and that "sum" applies to both lists and atomic values.

The set of Lisp methods defined in Tamgu is also available, as well as the methods available for lists, such as "size" for example.

# addition
(+ mat[6:1] mat[7:1] mat[10:1] ) 

# Rest of a division
(% mat[6:1] mat[7:1])

# logarithm Nepean
(ln mat[9:1] )

# You can encapsulate your formulas
(* 2 (+ mat[6:1] mat[7:1] mat[10:1]))

# Apply a change to a list and add it up #
(sum (_map '(* 2) mat[:1][6:10])) 

# To test a cell content
(if (>= mat[8:2] 60) 20 0)

Lambdas

You can also define lambda functions in your cells and perform any calculation you want including recursions.

# A simple function to calculate the average of a list
((lambda (x) (/ (sum x) (size x))) mat[1:][1:10])

# You can also implement recursions

( lambda (x) 
     (if (eq x ()) 
         0 
         (+ 
            (* 2 (car x)) 
            (self (cdr (cdr x)))) 
  mat[:1][6:10])

Functions

When you define a function with "defun" in a cell such as:

(defun avg(x) (/ (sum x) (size x))

The cell then takes the name of this function. You can now use it in your sheet as a new operator:

(avg mat[:1][1:8]) 

Additional functions

Functions can also be defined directly in tamgucalc code and then used in your sheets. Note that if you want to share your leaves, you will need to share these functions as well. A number of them has already been added as examples:

(average selection)

average calculates the average of a selection: (average mat[:1][1:10])

(fillcolumn selection cell_i cell_j)

fillcolumn fills the column starting at cell_i, cell_j with the contents of selection.

(fillcolumn (spell mat[:1][1:10] false) 1 2) fills column 2, starting from row 1 with the sorted data of the selection.

(fillrow selection cell_i cell_j)

fillrow fills the line starting at cell_i, cell_j with the contents of the selection.

(fillrow (sort mat[:1][1:10] false) 1 2) fills row 1, from column 2 with the sorted data of the selection.

(upto selection val)

upto retrieves the sub-list in selection up to the value val.

(fillcolumn (sort (upto mat[:1][10:] 0) false) 1 2): fills column 2 with the list of values in column 1 up to the first 0.

Note the use of mat[:1][10:] to extract the full column.

Basic commands

tamgucalc offers some options:

  • Control+e: edit a formula
  • Control+g: goto a cell. Separate row from column with a ",": r,c
  • Control+s: Save
  • Control+w: Save as
  • Control+d: Save raw data.
  • Control+r: allows you to change the number of rows and columns displayed on screen (r:c)
  • Control+q: Quit

Install tamgucalc

You can find "tamgucalc" at: tamgucalc.tmg

To run "tamgucalc" you must also get the "tamgu" interpreter, precompiled versions of which are available at:

To launch tamgucalc: tamgu tamgucalc.tmg (file)

Note that file is optional...

Clone this wiki locally