Functional programming language
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
COPYING
README.md
example.nil
quicksort.nil
test.go

README.md

nil

Functional programming language

Nil (which stands for '(not in lisp)) is a small, functional language with a bytecode compiler and virtual machine. It's very much a work in progress, with no fixed specification for even the semantics.

Types

Types are integers (bignums), floats (double precision), strings, lists and functions. The literals for the first three are what you'd expect. Lists are created using the builtin list function.

Forms

  • Atom

     12.5
     "hello"
     ...
    
  • Function call

     (function argument)
     (function argument argument)
     ...
    
  • Definition

     (def name value)
    
  • Anonymous Function

     (fun (x) (* x x))
     (fun (x y) (* y x))
     (fun xs (car xs))
    
  • Conditional

     (if condition true-case false-case)
    
  • List constructor

     (list)
     (list 1)
     (list 1 2 3 4)
    

To define a function f(x) = x², you'd say

    (def f (fun (x)
        (^ x 2)))

This is a function that calculates the product of a list of numbers:

    (def product (fun (m)
        (if m
                (* (car m) (product (cdr m)))
            1)))

Where (car x) returns the first item of the non-empty list x (the head) and (cdr x) returns a new list containing all but that first item (the tail).

For a more complicated example, here's the McCarthy 91 function and a loop that calls it 16 times with a value of 99:

(def M (fun (n)
    (if (> n 100.0)
            (- n 10.0)
        (M (M (+ n 11.0))))))

(def loop (fun (n)
    (print (M 99.0))
    (if (> n 1.0) (loop (- n 1.0)) 0.0)))

(loop 16.0)

Bultins

(> a b)     - defined on numbers
(= a b)     - defined on numbers
(+ a b)     - defined on numbers and lists
(- a b)     - defined on numbers
(* a b)     - defined on numbers
(/ a b)     - defined on numbers
(^ a b)     - defined on numbers
(print a)   - defined on everything
(car a)     - defined on lists
(cdr a)     - defined on lists

Running

To compile the source code, and after installing "go" from your distributions package manager (e.g. sudo apt-get install golang), run

go build test.go

Then run programs by typing

./test example.nil