From: ············@googlemail.com
Subject: How to use Lisp function in C++ program ?
Date: 
Message-ID: <2772b441-a6bc-466c-8c0e-2c1c4fb7000f@l32g2000hse.googlegroups.com>
Hi,

I want to use defined lisp function in C++ program.

I have C++ program as below:

#include <iostream>
using namespace std;

int main()
{
    cout << "Hello";
}



I have lisp function defined in example.lisp as below :

(defun my-append (list1 list2)
  (if (null list1)
      list2
      (cons (car list1) (my-append (cdr list1) list2))
  ))


I want to use my-append function from example.lisp
in above C++ program for appending two lists. How
I can implement this?

Thank you very much.

Mahavir

From: Lars Rune Nøstdal
Subject: Re: How to use Lisp function in C++ program ?
Date: 
Message-ID: <046bf762-681b-46e2-b94b-856ccd8883f2@d21g2000prf.googlegroups.com>
On Jan 29, 9:37 pm, ············@googlemail.com wrote:
> Hi,
>
> I want to use defined lisp function in C++ program.


····@ibmr52:~/programming/lisp$ cat call-lisp-from-c.c

typedef int(*SomeFunction)(int, int);

int doIt(SomeFunction func, int a, int b)
{
  return func(a, b) * 2;
}

····@ibmr52:~/programming/lisp$ gcc -g -Wall -fPIC -c call-lisp-from-
c.c  &&  gcc -shared -o libcall-lisp-from-c.so call-lisp-from-c.o
····@ibmr52:~/programming/lisp$


(eval-when (:execute :load-toplevel :compile-toplevel)
  (require :cffi))

(defpackage :clfc
  (:use :cl :cffi))
(in-package :clfc)

(load-foreign-library "libcall-lisp-from-c.so")


;; This is a function C will call in this example.
(defcallback aLispFunction :int ((a :int) (b :int))
  (format t "aLispFunction: ~A + ~A = ~A~%" a b (+ a b))
  (+ a b))



(defun main (a b)
  (foreign-funcall "doIt" :pointer (callback aLispFunction) :int
a :int b
                   :int))



Trying it out:

clfc> (main 3 4)
aLispFunction: 3 + 4 = 7
14
clfc> (main 2 2)
aLispFunction: 2 + 2 = 4
8

--
mvh, Lars Rune Nøstdal
From: Kaz Kylheku
Subject: Re: How to use Lisp function in C++ program ?
Date: 
Message-ID: <fb10ac4d-7014-4582-8cde-9d968414fc53@d70g2000hsb.googlegroups.com>
On Jan 29, 12:37 pm, ············@googlemail.com wrote:
> Hi,
>
> I want to use defined lisp function in C++ program.
>
> I have C++ program as below:
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
>     cout << "Hello";
>
> }
>
> I have lisp function defined in example.lisp as below :
>
> (defun my-append (list1 list2)
>   (if (null list1)
>       list2
>       (cons (car list1) (my-append (cdr list1) list2))
>   ))
>
> I want to use my-append function from example.lisp
> in above C++ program for appending two lists. How
> I can implement this?

Well, where are your lists that you want to append? I don't see them
anywhere in the C++ program. What do they look like?

The function works exclusively with Lisp datatypes that don't have any
straightforward counterpart in C++.

The conversion between some C++ lists and Lisp lists will be more
expensive than the work done by my-append (your idiotic implementation
thereof notwithstanding).

It's probably much more efficient to append your C++ lists in, doh, C+
+, rather than convert them to Lisp objects, do a foreign call out to
Lisp, and then convert the result back.

I hope you are trolling, because it would comfort me to know that this
type of stupidity is only faked as a joke.
From: Maciej Katafiasz
Subject: Re: How to use Lisp function in C++ program ?
Date: 
Message-ID: <fno3p8$1jm$7@news.net.uni-c.dk>
Den Tue, 29 Jan 2008 12:37:18 -0800 skrev mahavir.naik:

> I want to use defined lisp function in C++ program.
> 
> I have C++ program as below:
> 
> #include <iostream>
> using namespace std;
> 
> int main()
> {
>     cout << "Hello";
> }
> 
> I have lisp function defined in example.lisp as below :
> 
> (defun my-append (list1 list2)
>   (if (null list1)
>       list2
>       (cons (car list1) (my-append (cdr list1) list2))
>   ))
> 
> 
> I want to use my-append function from example.lisp in above C++ program
> for appending two lists. How I can implement this?

Your question makes no sense. First, there are no lists in your hello 
world. Second, you can't just take a lisp function and use it to "append 
lists" in a C++ programme. Those are completely different runtimes with 
completely different data structures, sharing on this level is impossible 
and impractical. You'd have to define an interchange format that both 
sides understood, and you'd have to marshall at least a bit when calling 
from one language into the other. That's something you do for well-
defined external library interfaces, not for trivial utility functions 
like append.

Cheers,
Maciej
From: vanekl
Subject: Re: How to use Lisp function in C++ program ?
Date: 
Message-ID: <fno4g3$co3$1@aioe.org>
············@googlemail.com wrote:
...
> I want to use my-append function from example.lisp
> in above C++ program for appending two lists. How
> I can implement this?
> 
> Thank you very much.
> 
> Mahavir

You might be interested in
http://chicken.wiki.br/crunch
but I would just learn the STL if you have to stay in C++,
or proto in CL and port to STL after the prototyping is done.
Lou
From: Edi Weitz
Subject: Re: How to use Lisp function in C++ program ?
Date: 
Message-ID: <uodb4e4qc.fsf@agharta.de>
On Tue, 29 Jan 2008 12:37:18 -0800 (PST), ············@googlemail.com wrote:

> I want to use my-append function from example.lisp in above C++
> program for appending two lists. How I can implement this?

You can use a Lisp implementation that can create shared libraries
(DLLs) and export (the names of) your Lisp functions from there.  A
C++ program can then load the shared library and call these functions
as if they were C functions.  LispWorks and AllegroCL can both do
that, and I think Corman Lisp as well.

Here are examples of "plug-ins" for the FileMaker database which were
created with LispWorks.  FileMaker plug-ins are actually DLLs.

  http://jensteich.de/exif-plugin/
  http://jensteich.de/regex-plugin/

Edi.

-- 

European Common Lisp Meeting, Amsterdam, April 19/20, 2008

  http://weitz.de/eclm2008/

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Pascal J. Bourguignon
Subject: Re: How to use Lisp function in C++ program ?
Date: 
Message-ID: <7czlun3av1.fsf@pbourguignon.anevia.com>
············@googlemail.com writes:

> Hi,
>
> I want to use defined lisp function in C++ program.
>
> I have C++ program as below:
>
> #include <iostream>
> using namespace std;
>
> int main()
> {
>     cout << "Hello";
> }
>
>
>
> I have lisp function defined in example.lisp as below :
>
> (defun my-append (list1 list2)
>   (if (null list1)
>       list2
>       (cons (car list1) (my-append (cdr list1) list2))
>   ))
>
>
> I want to use my-append function from example.lisp
> in above C++ program for appending two lists. How
> I can implement this?

Using ecls.  http://ecls.sourceforge.net/

http://www.den.rcast.u-tokyo.ac.jp/~salvi/archive/snippets/ecl-test.html
http://www.den.rcast.u-tokyo.ac.jp/~salvi/archive/snippets/embedded.html

So basically, you write:


int main()
{
   eval("(defun my-append (list1 list2)
            (if (null list1)
               list2
               (cons (car list1) (my-append (cdr list1) list2))))");
   eval("(print (my-append '(you should learn) '(to use google more))"));
   return(0);
}


-- 
__Pascal Bourguignon__
·························@anevia.com
http://www.anevia.com