Quick Start Guide to L Sharp

Introduction

L Sharp .NET is a new way to program the .NET framework. Primarily intended as a scripting language, it uses a Lisp dialect similar to Arc but tightly integrates with the .NET Framework (contrary to Arc thinking!) to give a rich set of libraries.

Currently L Sharp only ships with an interpreter, but the intention is to build a compiler which produces .EXE files.

You may wish to go and read a LISP or SCHEME primer before reading this document.

Starting L Sharp

C:\LSharp\>LSharp.exe

Running L Sharp with no arguments starts an interactive session with the interpreter.

Welcome to L Sharp .NET, a powerful lisp-based scripting language for .NET.
Copyright (C) 2005 Rob Blackwell & Active Web Solutions.

This program is free software and is distributed under the terms of
the GNU General Public License.

For more information, see www.lsharp.org

Build 1.11.2164.29780
Microsoft Windows NT 5.1.2600 Service Pack 2
CLR 2.0.50727.42

>
    

When L Sharp starts, it prints a welcome message and gives you a single chevron - this is the toploop prompt.

Evaluating expressions

Whatever you type at the toploop gets evaluated by Eval, the L Sharp evaluator. Numbers evaluate to themselves.

> 21
21
>

Strings evaluate to themselves.

> "Hello World"
"Hello World"
>

An expression consists of an atom or a list. If expression is a list, then the first argument is assumed to be a function, special form or a .NET method call. Here, the function + is applied to three arguments and the evaluator adds those arguments together.

> (+ 10 20 30)
60
>

Here the function is really a .NET method call.

> (writeline console "Hello World")
Hello World
null
> 

Hello World is printed because that's what System.Console.WriteLine does. null is just the return value from the expression, in the same way that 60 was the return value in the previous example.

L Sharp as a Scripting Engine

If you invoke LSharp.exe with a parameter, the name of an LSharp source file, then the source file runs as a script and exits. This allows you to conveniently write and run command line utilities and programs.

C:\LSharp\>LSharp hello.ls
Hello World
C:\LSharp\>

Things you Should Know

L Sharp is not case sensitive; you can mix upper and lower case. I suggest using all lower case in accordnace with the best practice thinking on Arc. Microsoft .NET is case sensitive, but L Sharp does case insensitive matching on types and methods (like VB.NET), so for example System.Console is the same as system.console.

Whilst the dialect is similar to ARC, it's not the same. Reading the Arc materials is worthwhile, but be warned of differences.

All numbers are of type System.Double or System.Int32

null is used in a similar way to Common Lisp's NIL.

If you are reading this then you are an early adopter - the language is still changing and future backwards compatibility is not guaranteed!

In the same way that C Sharp isn't C and J Sharp isn't Java, L Sharp isn't Lisp; you can recognise the parentage but they aren't the same thing.

Hello World

Tradition says that the first example should be Hello World

(WriteLine Console "Hello World")

You'll notice that its similar to a C Sharp Hello World, except that it doesn't have all the class baggage.

Essentially we are calling the static method "WriteLine" on the "System.Console" object, passing a parameter, the string "Hello World".

Because writing to the console is such a frequent task, the function prl meaning "print line" is defined as a short hand.

(prl "Hello World")

Here is a similar program, GUI style

(reference "System.Windows.Forms")
(using "System.Windows.Forms") 

(Show MessageBox "Hello World" "L Sharp")

The reference function loads an assembly (in this case System.Windows.Forms) from the GAC, or from a specified file. The using directive permits the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace. The last line is the equivalent of MessageBox.Show ("Hello World", "LSharp") in C Sharp.

Defining a Variable

( = foo 10)

This assigns the vale 10 to the variable foo. Note that foo does not have to be explicitly declared, nor does it have a specified type; in LSharp values have types, not variables.

Evaluating

(+ foo 50)

returns 60.

Defining a Function

Most languages make you name a function; in LSharp functions are named by assigning them to variables.

(fn (x) (* x x))

declares a function that returns the square of its argument. It may be convenient to assign it to a variable

(= square (fn (x) (* x x)))

and then it can be used thus

(square 10)

Treating functions this way makes them first class. They can be assigned to variables and passed as parameters in just the same way as any other object. That turns out to be really useful. (See the Directory.ls sample for an example).

Old Lisp hackers may prefer to use the defun macro as a shorthand:

(defun square (x) (* x x))

Loading a Library

You can split your application across multiple files and create libraries. One source code file can effectively include another using load

(load "c:\\files\\library.ls")

Iteration

L Sharp includes a number of iteration constructs.

;;; Demonstrate some L Sharp iteration expressions

(for (= i 0) (< i 10) (++ i)
	(pr i))
	
(let i 0
	(while (< (++ i) 10) (pr i)))
	
(to i 10 (pr i))

(each x '(a b c) (pr x))

A particularly useful idiom in LSharp is the ability to use each on any List or object that is IEnumerable.

		
> (each c "Hello" (prl c))
#\H
#\e
#\l
#\l
#\o
null
> 

Embedding L Sharp

The LSharp.dll assembly is released under the LGPL precisely so that you can embed it into your own project without necessarily making your project open source. It's an ideal way of making your applications scriptable.

using LSharp;
...
object o = Runtime.EvalString("(+ 10 10)");
...

Next Steps

Looking at the Samples and Examples section in this documentation is probably the best place to get started. The L Sharp Language Reference section gives a full breakdown of all functions and special forms. Additionally, I'm hoping to start a repository of useful L Sharp programs at http://lsharp.sourceforge.net/download/.