L Sharp LogoL Sharp .NET

Version 2 (Experimental) Now Available!

L Sharp .NET is a Lisp-like scripting language for .NET. It uses a modern Lisp dialect and integrates with the .NET Framework which provides a rich set of libraries.

It has a small, simple, extensible core that's coded in C#. The source code is easy to follow and you can easily add your own functions in C# or L#.

What's New?

Version 2 is a major new rewrite with lots of new features including a compiler so we get a big performance gain over version 1.

Distributed under the terms of the BSD licence, so you can pretty much do what you like with it.

New and updated syntax including dot notation for .NET calls (console.writeline "Hello World").

Getting Started

At the moment, L Sharp 2 is alpha quality and unreleased; you'll need to grab the source code and compile it yourself.

svn co https://lsharp.svn.sourceforge.net/svnroot/lsharp/v2 lsharp 

Load the solution in Visual Studio 2008 and build.

Right click LSharpConsole and set as StartUp project.

You'll also need NUnit http://www.nunit.org/ if you want to compile and run the tests.

Quick Tutorial

LSharp programs consist of expressions. The simplest expressions are things like numbers and strings, which evaluate to themselves.

> 1
1
> 3.14
3.14
> "Hello World"
"Hello World"
>

Expressions enclosed in parenthesis are lists. When a list is evaluated, all the elements are evaluated from left to right. The value of the first element is assumed to be a function and it is passed the values of the other elements as arguments. Thus:

> (+ 1 2)
3

First +, 1, and 2 get evaluated, returning the plus function, 1, and 2 respectively. Then 1 and 2 get passed to the plus function, which returns 3.

Because we're using prefix notation, plus can have an arbitrary number of arguments.

> (+ 10 20 30)
60

Plus is overloaded to work with strings too.

> (+ "Hello" "World")
"HelloWorld"

You can call methods from the .NET framework in a similar way.

> (Console.Writeline "Hello {0}" "world")
Hello world
null
> (.hour (datetime.now))
8

Sometimes it's convenient to get hold of the value of the last evaluation. Just use *1.

> (+ 1 2)
3
> *1
3

Similarly *2 is the last but one evaluation. *e is the last exception.

LSharp has lots of built in functions, you can get a quick list like this:

> (map (fn (x) (prn (.key x))) environment)

You can get help on any function using the help function

> (help sleep)
(n) : Sleeps for n seconds
LSharp.Function

You can define your own functions with def:

> (def fact(n)
        (if (is n 0) 1 (* n (fact (- n 1)))))
LSharp.Function
> (fact 5)
120

Most LISPs allow car and cdr or first and rest on lists. L Sharp allows these on any arbitrary sequence including strings and arrays.

> (first '( 1 2 3))
1
> (rest '(1 2 3))
(2 3)
> (first "Hello")
#\H
> (rest "Hello")
(#\e #\l #\l #\o)
> (first [1 2 3])
1
> (rest [ 1 2 3])
(2 3)

If you're going to much work with LSharp you probably want an editor that does parenthesis mapping. I use Emacs and put the following in my .emacs file

;; Support for L Sharp
(show-paren-mode t)
(add-to-list 'auto-mode-alist '("\\.ls$" . lisp-mode))
(setq inferior-lisp-program "cmd /c  C:\\LSharp\\LSharpConsole\\bin\\Debug\\LSharpConsole.exe")

Next Steps

There isn't much documentation available yet, so as an early adopter, you'll need to dig into the source code and poke around. Look in Runtime.cs where all the functions and Macros are defined. Also look at the NUnit tests and Samples directories.

You may also be interested to look at Paul Graham's Arc language or Rich Hickey's Clojure.

Google Groups Please share your feedback and ideas on The LSharp Google Group.

Please remember that LSharp is still unreleased and I reserve the right to change the syntax without necessarily keeping backwards compatibility.

Apologies in advance: I don't always get time to respond to all emails - sorry.

Enjoy !

Rob Blackwell
November 2008.