L Sharp Language Reference

Introduction

This is the definitive guide to the L Sharp language.

Atoms and Lists

In L Sharp everything is either an atom or a list. Atoms are things like numbers, symbols and strings. Lists are represented using parenthesis. (a b c). Lists can contain atoms or lists or any combination thereof.

The Nature of True and False

L Sharp takes a fairly liberal view of true and false. The boolean true is self evident true; in the same way false is false. 0 and null are also considered false. Any other object is considered true.

Atoms

null

null

The null keyword is a literal that represents a null reference, one that does not refer to any object. null is the default value of reference-type variables. This is NIL in Common Lisp.

true

true

Evaluates to the boolean value true

false

false

The Evaluates to the boolean value false

Symbols

foo

A symbol that has previously been set in the environment, returns its value, e.g.

(do (= foo 5) foo)

returns 5. Unbound symbols evaluate to themselves.

Integers

56

Integers evaluate to objects of type Int32.

Doubles

3.14

Decimal numbers evaluate to objects of type Double.

Strings

"Hello World"

Strings evaluate to objects of type System.String.

Functions, Closures, Macros and Special Forms

Functions, closures, macros and special forms may all be applied to arguments using expressions of the form

(function arguments)

. When a built in L Sharp function is called, all arguments are first evaluated using eval and then passed as parameters to the function. (Interested readers should look at the Functions.cs file in the LSharp source to see how functions can be implemented, and how easy it is to extend the language yourself).

A closure is a function defined and implemented in the L Shasp language using fn. For example

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

. When you call a closure

(square 5)

all arguments are first evaluated using eval and then passed to the closure.

A macro is is implemented within L Sharp using macro.

(= nil! (macro (x) `(== ,x null)))

When a macro is called, the macro is first expanded using macroexpand and then evalauted in a similar way to a closure above. i.e. all arguments are first evaluated using eval before being passed.

A special form is similar to a function,except that the expressions given as arguments are passed to the special form iplementation without being evaluated. This allows the special form to effect the evaluation process as necessary. As an example, consider

(= foo 5)

. If = was implemented as a function, then foo would be evaluated before execution and we would simply have its value. It wouldnt be possible to perform the assignment. Implementing = as a special form allows us to treat foo in a special way, as a named symbol and be able to perform the necessary assignment. (Interested readrs are reffered to the SpecialForms.cs file in the source code for implementations of all L Sharp special forms).

Special Forms

and

(and expression*)

Evaluates the expressions in order, returning true if all expressions are true, and false if any single expression is false. If a false expression is encountered, then the remaining expressions are not evaluated (short circuit evaluation).

backquote

(backquote expression)

Returns an expression created by expanding any backquote expressions in the given expression. Backquote syntax is often used in macros and is well documented in various Common Lisp references.

> (= x '(a b c))
(a b c)
> `(,x)
((a b c) )
> `(,@x)
(a b c)
> 

call

(call method object argument*)

Calls a .NET method on a given object with given arguments. This is useful if the method name clashes with a variable which is already bound in the current L Sharp lexical environment.

compile

(compile filename expression*)

Compiles the given stament to IL, storing the result in a new EXE packaged assembly filename. (Actually this dosnt work yet).

cond

(cond (test expression)* [default])

Evaluates tests until one returns true. If a test returns true, then evaluates its corresponding expression. If no test return true, then and optionally specified default expression is evaluated.

--, decrement

(-- symbol)

Subtracts one from the variable represented by symbol. A shorthand for

(= symbol (- symbol 1))

defclass

(defclass ...)

Reserved for future use.

do

(do expression*)

Evaluates each expression, one at a time. Returns the result of the evaluation of the last expression.

each

(each symbol IEnumerable expression)

Iterates over any object which impelements IEnumerablewith succesive elements being assigned to a variable named symbol; exceutes expression on each iteration. Cons (LSharp lists), as well as many .NET collections are IEnumerable. Foreach is a synonym for each.

fn

(fn arguments body)

Defines a closure with the specified list of arguments and the specified body, an L Sharp expression. Not unlike a lambda expression in Common Lisp.NB arguments is a simple list, we dont yet support keyword or optional arguments.

for

(for initialiser test iterator statement)

The for special form corresponds to the for construct found in most algebraic programming languages. The initialiser is executed. The statement is executed while test is true. The iterator is executed at the end of each statement execution.

foreach

See Each.

if

(if test then [else])

The if special form corresponds to the if-then-else construct found in most algebraic programming languages. First the form test is evauated, if true then the form then is evaluated.Otherwise, optionally the form else is evaluated.

increment

(++ symbol)

Adds one to the variable represented by symbol. A shorthand for

(= symbol (+ symbol 1))

let

(let symbol value expression*)

Binds a new local variable symbol to value in a new local lexical environment, before evaluating expressions. Similar to with, but often more convenient for decalring a single local variable.

macro

(macro arguments expression*)

Defines a macro. Similar in some respects to defining a clsoure using fn except that the expressions are expanded using macroexpand in the current environment before being evaluated.

or

(or expression*)

Evaluates the expressions in order, returning true if any expression is true, and false if all expressions are false. If a true expression is encountered, then the remaining expressions are not evaluated (short circuit evaluation).

quote

(quote object)

Returns object without evaluating it. object may be any .NET object. The shortend form 'object is more usual.

setf

          (= { symbol value}*)
        

Setf (Set Form) is the variable assignment operator. Sets each variable symbol to value in the current environment. Currently the same as setq. The abbreviation = is more commonly used in L Sharp.

setq

(= { symbol value}*)

Setq (Set Quote) is the variable assignment operator. Sets each variable symbol to value in the current environment. The abbreviation = is more commonly used in L Sharp.

the

(the type value)

Returns value converted or cast to an object of the specified type. Throws an exception is the cast is not achievable.

The allows type casting and type conversion. This is much more than a wrapper for the System.Convert class, it has special meaning for conversions to and from Lists and certain common .NET data structures. The following are of particular interest:

(the Hashtable list)
(the Queue queue)
(the Stack stack)
(the Cons dataTable)
(the SortedList list)
(the Cons sortedList)
(the Cons anyCollection)

to

(to variable limit expression)

Starting at 0, assigns variable to succesive integers upto and including limit. Executes expression on each iteration.

trace

(trace filename expression*)

Traces an evaluation of expression* (as if in an implicit do), documenting all call and return steps; writes the output as an XML file in filename.

try

(try expression catch [finally])

The try special form corresponds to the try-catch-finally construct found in C Sharp. If catch is null then there is deemed to be no catch block at all. If an exception occurs, the variable it is bound to the Exception object in the local environment.

when

(when test expression*)

Similar to if, but more convenient if there is no else case. Evalautes expressions if the evaluation of test is true.

while

(while test expression*)

The while special form corresponds to the while construct found in most algebraic programming languages. First test is evauated, if true then expression* is evaluated. The process continues until the evaluation of test is false.

with

(with ([symbol value]* ) expression*)

Binds new local variables symbols to values in a new local lexical environment, before evaluating expressions. Similar to let, but allows multiple local variables to be bound.

(with (x 'a y 'b)
	(list x y))

Built in Functions

Add, +

(+  object*)

Returns the sum of all the specified objects. Each object must be a numerical type usch as System.In32 or System.Double.

append

(append list1 list2 .. listN)

Returns a new list which is a concatenation of all the lists supplied as arguments. All lists are copied (as if by copy-list) except the last list.

> (append '(a b c) '(d e f) '(g h j))
(a b c d e f g h j)

apply

(apply function arguments)

Applies function to a list of arguments. function may be a built-in lsharp function, a closure defined by fn, a macro defined using macro or the name of a method in the .NET framework.

assoc

(assoc item list)

Searches the list for a pair who's car is equal to item. If a match is found then the pair is returned, otherwise null. Depending on the size of your list, and the number of times you want to assoc it, you may find it more efficient to use a hashtable. The following is similar to assoc.

(using "System.Collections")
(= foo (the Hashtable '((a b) (c d) (e f))))
(Item foo 'c)

caaar

(caaar list>)

Returns the caaar of the list. A shorthand for (car (car (car list))).

caadr

(caadr list>)

Returns the caadr of the list.

caar

(caar list)

Returns the caar of the list.

cadar

(cadar list)

Returns the cadar of the list.

caddr

(caddr list)

Returns the caddr of the list.

cadr

(cadr list)

Returns the cadr of the list.

car

(car list)

Returns the car of the list. The car is the first element of the list.

cdaar

(cdaar list)

Returns the cdaar of the list.

cdar

(cdar list)

Returns the cdar of the list.

cddar

(cddar list)

Returns the cddar of the list.

cdddr

(cdddr list)

Returns the cdddr of the list.

cddr

(cddr list)

Returns the cddr of the list.

cdr

(cdr list)

Returns the cdr of the list. Also know as the rest of the list.

cons

(cons object1 object 2)

Returns a new cons with object1 as the car and object2 as the cdr.

copy-list

(copy-list list)

Returns a shallow copy of list. Only the list structure of list is copied; the elements of the resulting list are the same as the corresponding elements of the given list.

Divide /

(/object*)

Divides object1 by object2 upto objectn. The objects are assumed to be numeric types.

environment

(environment)

Returns the current, local environment object - an instance of LSharp.Environment. This is occasionally useful for debugging and manipulation purposes.

eq

(eq expression*)

Returns true if all expressions are reference equal, that is they refer to the same object in memory. As a special case, null is eq to null.

eql, (or ==)

(eql expression*)

Returns true if all expressions are equal, that is their implementations of equal return true. As a special case, null is eql to null.

eval

(eval expression)

Calls on the L Sharp interpreter to evaluate the given expression

evalstring

(evalstring string)

Calls on the L Sharp interpreter to evaluate the given expression which is represented as a string. E.g.

(evalstring "(+ 1 2)")

. Because the evaluation takes place in the current environment, the expression contained in the string can reference variables which have already been defined in that environment.

exit

(exit [exit-code])

Terminates this process and gives the underlying operating system the specified exit-code.

first

(first list)

Returns the first element of the list. (Also known as the car)

Greater Than, >

(> object1 object2 object*)

Returns true if object1 is greater than object2, object2 is greater than object3 and so on.

Greater Than Or Equal, >=

(>= object1 object2 object*)

Returns true if object1 is greater than or eql to object2, object2 is greater than or eql toobject3 and so on.

inspect

(inspect object)

Returns a description of the specified object, using reflection. Useful for debugging.

is

 (is type object)

Returns true if object is an instance of the specified type or supertype.

length

 (length expression)

Returns the length of expression. If expression is null, length returns 0, otherwise the length is calculated by calling the length method on the object, ensuring that length works for strings, lists and most collection-like objects.

Less Than, <

(< object1 object2 object*)

Less than

Less Than Or Equal <=

(<= object1 object2 object*)

Less than or equal

list

(list object*)

Creates a new cons, an order list with each object as a member.

load

(load filename)

Loads and evaluates all statements in the given filename which must be a text file. Remember that backslash characters must be escaped within strings, so a typical example would be something like

(load "c:\\data\\myfile.lsp")

Logical And &

(& expression*)

Returns the bitwise logical and of its arguments

Logical Inclusive Or |

(| expression*)

Returns the bitwise logical inclusive or of its arguments

Logical Exclusive Or ^

(^ expression*)

Returns the bitwise logical exclusive or of its arguments

macroexpand

(macroexpand macro)

Expands the given macro in the current lexical environment.

map

(map function list)

Applies function to each element in list return a new list of return values.

Multiply, *

(* object*)

Returns the result of multiplying each object together. Each object is assumed to be a numeric value.

nconc

          (nconc list*)
        

Returns a list whose elements are the elements of each list in order. Destructively modifies all but the last list, such that the cdr of the last cons in each list is set to the next list.

new

(new class)

Creates a new object, an instance of type class

not

(not bool)

Returns the logical complement of the given boolean.

Not Equal, !=

(!= expression*)

The logical complement of the eql operator.

nth

(nth n IEnumerable)

Returns the nth element of the IEnumarable object for n >=0. It is important to note that the numbering starts at 0 which is the first element.

pr

(pr object*)

Prints each object to Console.Out, without a new line. This is really just a shorthand for (Write Console object)

prl

(prl object*)

Prints each object to Console.Out, then prints a new line. This is really just a shorthand for (WriteLine Console object)

read

(read stream [eof-value])

Calls the L Sharp reader to turn textual input from the steam into objects suitable for evaluation by eval. The default eof-value is null, but you can specify any eof-value object to avoid ambiguity.

reference

(reference name*)

Loads the specified .NET assembly, either from the GAC, local directory or from and explicit directory. name may either be a fully qualified filename or a partial assembly name.

reset

(reset)

Resets the global environment.

reverse

(reverse list)

Reverses the given list.

> (reverse '( 1 2 3))
(3 2 1)

rest

(rest list)

A synonym for cdr.

Subtract, -

(- object*)

Returns the result of subtracting object2 from object1 etc. objects are assumed to be numeric values.

throw

 (throw exception)

Throws a .NET exception, exception must be a System.Exception or derrivitive.

typeof

(typeof symbol)

Returns the System.Type whose name is that of the symbol. (i.e. coverts symbol names to type objects).

(typeof Console)

using

(using namespace)

Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace.

Built in Macros

defun

(defun name args body)

A shorthand for

(= name (fn args body)))

defmacro

(defmacro name args body)

A shorthand for

(= name (macro args body))

listp

(listp arg)

Returns True if arg is a list, False otherwise