views:

296

answers:

3

Hello,

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 this? 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.

+4  A: 

Have you looked into Clojure? It's a Lisp dialect that runs on the Java Virtual Machine.

John Y
...and http://java.dzone.com/tips/calling-clojure-java describes how to call Clojure code from Java.
itowlson
Clojure looks preety good if i will find that it is backcompatible with Lisp, but for the moment i can't load even the most simple example, i keep getting a `Exception in thread "main" java.io.FileNotFoundException: Could not locate Clojure resource on classpath: X:\t.clj`and all i have is : ` RT.loadResourceScript("X:\\t.clj");` and the file is there.
theBlinker
Nevermind, it seems that if i add the file in the Clojure directory works fine. There is some talk on the net about editing the pom.xml file
theBlinker
Sadly it is not Lisp backward compatible
theBlinker
+7  A: 

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.

Rainer Joswig
That looks alright, trying it now
theBlinker
This works perfectly and is what i was looking for. Thank youFor anyone that might find this of use i am posting a quick [crash course](http://www.automatous-monk.com/jvmlanguages/abcl/Armed_Bear_Common_Lisp.html) i found.
theBlinker
+2  A: 

FOIL (Foreign Object Interface for Lisp) provides means to interact with Java and .NET programs.

dmitry_vk