Lisp code called from Java
Long story:
I am doing a project for my functional programing class, and I thought of writing an AI controller in Lisp, for the Mario AI competition.
I was looking over frameworks/libraries/ways of calling Lisp code from Java, or even better Lisp-Java intercommunication.
I have looked at Jacol, but it is old, and it does not compile fine for me.
My best choice so far is: Jatha. It is really neat, although some lisp constructs are not yet implemented, one can easily define his own constructs. For example mapcar and cond are not implemented. I have implemented my own mapcar, named mapp in Lisp like this:
(defun map* (f l r)
(if (null l)
r
(map* f (rest l) (cons (funcall f (first l)) r))))
(defun mapp (f l)
(reverse (map* f l nil)))
Now I have a simple function that uses this, for example, a function that numbers how many atoms there are in a non-linear list
(defun myfunc (l)
(if (atom l)
'1
(apply '+ (mapp 'myfunc l))))
(myfunc '(6 2))
This all works fine in clisp Now to call Lisp code from Java I used Jatha. All one has to do is import the Jatha library in the java project and load a lisp file like this (ex):
import org.jatha.*;
import org.jatha.dynatype.*;
public class Main {
public static void main(String[] args) {
Jatha lisp = new Jatha(false, false);
lisp.init();
lisp.start();
LispValue file = lisp.makeString("X:\\lispproject\\test1.lisp");
LispValue rez1 = lisp.load(file);
}
}
While that code works fine in clisp, and other implementations, this code produces a StackOverflow
run:
APPLY: fn = +, args = ((1 1))
S: ((+ (QUOTE 1) (QUOTE 1)))
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Long.toString(Long.java:242)
at java.lang.Long.toString(Long.java:100)
at java.lang.String.valueOf(String.java:2946)
at org.jatha.dynatype.StandardLispInteger.toString(StandardLispInteger.java:113)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:174)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:153)
at org.jatha.dynatype.StandardLispCons.toString(StandardLispCons.java:152)
at org.jatha.dynatype.StandardLispCons.toStringAsCdr_internal(StandardLispCons.java:17
So my question is, why does it do this? Is my code wrong?
Is it a bug in Jatha? See for yourself, it doesn't take long to set up. Have you ever done something similar? Do you know any other better ways to do t开发者_C百科his? All I want is to call from Java some Lisp code, get it executed, and get back results computed by the Lisp code. Thanks.[Edit] Fixed code, pasted something wrong.
ABCL is a Common Lisp implementation that runs directly in the JVM - possibly it is the best option to run Common Lisp side by side with Java.
Have you looked into Clojure? It's a Lisp dialect that runs on the Java Virtual Machine.
FOIL (Foreign Object Interface for Lisp) provides means to interact with Java and .NET programs.
精彩评论