calling scheme procedure from java and passing a list as argument...

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

calling scheme procedure from java and passing a list as argument...

Damien Mattei
hello again,
suppose i have a java code (the case is running on tomcat web server but
that's make no difference)
and i want to pass a list to a scheme routine, i have two questions:
-is there a way to call a scheme procedure in kawa,i suppose the answer
is yes but how?
( i know i can do it making a method in a kawa class definition and call
the method)
- but how can i pass a list as parameter, what is the equivalent of
scheme list in java or kawa??
I would greatly appre"ciate a little example...

thanks
Damien
Reply | Threaded
Open this post in threaded view
|

Re: calling scheme procedure from java and passing a list as argument...

Per Bothner


On 09/17/2015 06:09 AM, Damien Mattei wrote:
> hello again,
> suppose i have a java code (the case is running on tomcat web server but
> that's make no difference)
> and i want to pass a list to a scheme routine, i have two questions:
> -is there a way to call a scheme procedure in kawa,i suppose the answer
> is yes but how?
> ( i know i can do it making a method in a kawa class definition and call
> the method)

This page talks about *evaluating* Scheme from Java:

http://www.gnu.org/software/kawa/Evaluating-Scheme-expressions-from-Java.html

If you have a Scheme Proecdure object you can call by using one of the
apply methods.  For example:

    Object[] args = {x, y};
    Object result = myProc.applyN(args);
or:
   Object result = myProc.apply2(x, y);

> - but how can i pass a list as parameter, what is the equivalent of
> scheme list in java or kawa??
> I would greatly appre"ciate a little example...

To create a list you can use one of the static methods in gnu..lists.LList,
like makeList or list1 ... list4.

It's usually best to do as much as you can in Scheme: It is easier (and
better documented with a more stable API) calling Java from Scheme
than calling Scheme from Java.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: calling scheme procedure from java and passing a list as argument...

Damien Mattei


Le 17/09/2015 20:54, Per Bothner a écrit :

>
>
> On 09/17/2015 06:09 AM, Damien Mattei wrote:
>> hello again,
>> suppose i have a java code (the case is running on tomcat web server but
>> that's make no difference)
>> and i want to pass a list to a scheme routine, i have two questions:
>> -is there a way to call a scheme procedure in kawa,i suppose the answer
>> is yes but how?
>> ( i know i can do it making a method in a kawa class definition and call
>> the method)
>
> This page talks about *evaluating* Scheme from Java:
>
> http://www.gnu.org/software/kawa/Evaluating-Scheme-expressions-from-Java.html
>
>
> If you have a Scheme Proecdure object you can call by using one of the
> apply methods.  For example:
>
>    Object[] args = {x, y};
>    Object result = myProc.applyN(args);
> or:
>   Object result = myProc.apply2(x, y);
>
>> - but how can i pass a list as parameter, what is the equivalent of
>> scheme list in java or kawa??
>> I would greatly appre"ciate a little example...
>
> To create a list you can use one of the static methods in
> gnu..lists.LList,
> like makeList or list1 ... list4.
>
> It's usually best to do as much as you can in Scheme: It is easier (and
> better documented with a more stable API) calling Java from Scheme
> than calling Scheme from Java.
unfortunalely this is a java project including scheme,not the reverse,
i tried to use list2 in this example but it complains that
gnu.lists.LList is not a pair,
in scheme a list is a pair.
i have this error:
Exception in thread "main" Argument #1 '(. #<not a pair>)' to 'car' has
wrong type (gnu.lists.LList) (gnu.lists.LList cannot be cast to
gnu.lists.Pair)
        at Vector2D.plus(Vector2D.scm:28)
        at helloworld.main(helloworld.java:30)
Caused by: java.lang.ClassCastException: gnu.lists.LList cannot be cast
to gnu.lists.Pair
        ... 2 more
when i try to run this code:

    LList glst = new LList();
    glst.list2(2,3);
    Object result = vec.plus(glst);

on this method definition in Vector2D class:
((plus L) ::int
   (+ (car L) (cadr L)))

this is of course for testing before doing serious work...



Reply | Threaded
Open this post in threaded view
|

Re: calling scheme procedure from java and passing a list as argument...

Damien Mattei
finally i understand my error,result of many years without coding in java...

here is the solution:

    LList glst = new LList();
    //glst = glst.list2(2,3); // works also
    glst = LList.list2(2,3);
    System.out.println("glst : "+glst);
    Object result0 = vec.plus(glst);
    System.out.println("result0 :"+result0);


    Pair pair = new Pair(2, LList.Empty);
    Pair pair2 = new Pair(3, pair);
    LList glst2 = new LList();
    glst2 = pair2;
    System.out.println("glst2 : "+glst2);
    Object result = vec.plus(glst2);
    System.out.println("result : "+result);

and the result:

glst : (2 3)
result0 :5
glst2 : (3 2)
result : 5

Le 18/09/2015 11:52, Damien Mattei a écrit :

>
> Le 17/09/2015 20:54, Per Bothner a écrit :
>>
>> On 09/17/2015 06:09 AM, Damien Mattei wrote:
>>> hello again,
>>> suppose i have a java code (the case is running on tomcat web server but
>>> that's make no difference)
>>> and i want to pass a list to a scheme routine, i have two questions:
>>> -is there a way to call a scheme procedure in kawa,i suppose the answer
>>> is yes but how?
>>> ( i know i can do it making a method in a kawa class definition and call
>>> the method)
>> This page talks about *evaluating* Scheme from Java:
>>
>> http://www.gnu.org/software/kawa/Evaluating-Scheme-expressions-from-Java.html
>>
>>
>> If you have a Scheme Proecdure object you can call by using one of the
>> apply methods.  For example:
>>
>>    Object[] args = {x, y};
>>    Object result = myProc.applyN(args);
>> or:
>>   Object result = myProc.apply2(x, y);
>>
>>> - but how can i pass a list as parameter, what is the equivalent of
>>> scheme list in java or kawa??
>>> I would greatly appre"ciate a little example...
>> To create a list you can use one of the static methods in
>> gnu..lists.LList,
>> like makeList or list1 ... list4.
>>
>> It's usually best to do as much as you can in Scheme: It is easier (and
>> better documented with a more stable API) calling Java from Scheme
>> than calling Scheme from Java.
> unfortunalely this is a java project including scheme,not the reverse,
> i tried to use list2 in this example but it complains that
> gnu.lists.LList is not a pair,
> in scheme a list is a pair.
> i have this error:
> Exception in thread "main" Argument #1 '(. #<not a pair>)' to 'car' has
> wrong type (gnu.lists.LList) (gnu.lists.LList cannot be cast to
> gnu.lists.Pair)
>         at Vector2D.plus(Vector2D.scm:28)
>         at helloworld.main(helloworld.java:30)
> Caused by: java.lang.ClassCastException: gnu.lists.LList cannot be cast
> to gnu.lists.Pair
>         ... 2 more
> when i try to run this code:
>
>     LList glst = new LList();
>     glst.list2(2,3);
>     Object result = vec.plus(glst);
>
> on this method definition in Vector2D class:
> ((plus L) ::int
>    (+ (car L) (cadr L)))
>
> this is of course for testing before doing serious work...
>
>
>
>