Getting Started with NexJ Scheme
To run the REPL interactively, first build the JAR file and then invoke it:
C:\> ant nexj-oss-scheme.jar C:\> java -jar nexj-oss-scheme.jar
Available ant tasks:
- nexj-oss-scheme.jar
- compile the Java files and bundle the object and source files into the JAR file
- doc
- generate Javadoc from the codebase
- clean
- remove all of the files generated by the ant tasks
Running the NexJ Scheme Interpreter
You start the interpreter from a console by executing the java -jar command. Once running, you interact with the NexJ Scheme interpreter simply by typing Scheme expressions:
C:\> java -jar nexj-oss-scheme.jar Oct 8, 2009 9:20:21 AM nexj.core.util.log.j2se.J2SELogger log INFO: Using system configuration properties ; NexJ Scheme > (+ 2 5) ; 7 > "hello world!" ; "hello world!" > (define msg "hello world!") ; "hello world!" > msg ; "hello world!" > (string-length msg) ; 12 > (* (string-length msg) 2) ; 24
Invoking Java from Scheme
With its core being written in Java, the NexJ Scheme engine allows transparent invocation of Java code while being free of typing.
NexJ Scheme uses Java reflection to examine all public methods and fields that are available on a Java class. The following transformations are performed on method names:
-
setPropertyX(Object value)is accessible in Scheme as(instance'propertyX value) -
getPropertyX()is accessible in Scheme as(instance'propertyX) -
isPropertyX()is accessible in Scheme as(instance'propertyX)
You can use the Scheme function (import <fullyQualifiedJavaClass>) to make that class
accessible to the Scheme engine. Once imported, you can refer to the variable
<fullyQualifiedJavaClass> to affect static fields, use methods, and invoke its constructor
using the 'new member. For example:
(import 'java.util.ArrayList) (define numbers (java.util.ArrayList'new)) (for ((i 0)) (< i 10) (set! i (+ i 1)) (numbers'add i) ) (for ((it (numbers'iterator))) (it'hasNext) () (write (it'next)) )
Using Scheme Libraries
You can load a file of Scheme commands using the load function. Say for example you have a file named utils.scm containing:
(import 'java.lang.System) (define (addemup a b) (+ a b)) (define exit (lambda () (java.lang.System'exit 0)))
Then you can use the functions addemup and exit with:
C:\> java -jar nexj-oss-scheme.jar Oct 8, 2009 9:22:13 AM nexj.core.util.log.j2se.J2SELogger log INFO: Using system configuration properties ; NexJ Scheme > (load "utils.scm") ; () > (addemup 3 4) ; 7 > (exit) C:\>
Exiting the NexJ Scheme Console
There are various ways to exit the interpreter. Depending on which console you are using, you may have different results:
- press Ctrl-C
- press Ctrl-D (possibly followed by ENTER)
- press Ctrl-Z (possibly followed by ENTER)
- run the script:
(java.lang.System'exit 0) - close the console window