Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to use Lisp function in C++ program ?

738 views
Skip to first unread message

mahavi...@googlemail.com

unread,
Jan 29, 2008, 3:37:18 PM1/29/08
to
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

Edi Weitz

unread,
Jan 29, 2008, 3:50:19 PM1/29/08
to
On Tue, 29 Jan 2008 12:37:18 -0800 (PST), mahavi...@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 "spam...@agharta.de" 5) "edi")

Maciej Katafiasz

unread,
Jan 29, 2008, 3:54:00 PM1/29/08
to

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

vanekl

unread,
Jan 29, 2008, 4:08:09 PM1/29/08
to
mahavi...@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

Lars Rune Nøstdal

unread,
Jan 29, 2008, 4:23:29 PM1/29/08
to
On Jan 29, 9:37 pm, mahavir.n...@googlemail.com wrote:
> Hi,
>
> I want to use defined lisp function in C++ program.


lars@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;
}

lars@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
lars@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

Kaz Kylheku

unread,
Jan 29, 2008, 9:18:17 PM1/29/08
to

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.


Pascal J. Bourguignon

unread,
Jan 30, 2008, 4:45:38 AM1/30/08
to
mahavi...@googlemail.com writes:

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__
mailto:pascal.bo...@anevia.com
http://www.anevia.com

yakup.1...@gmail.com

unread,
Jan 10, 2015, 5:36:42 PM1/10/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
29 Ocak 2008 Salı 22:37:18 UTC+2 tarihinde mahavi...@googlemail.com yazdı:
c++ programlama kitapları 0 dan baslangıs seviyesin itibaren 75 den fazla kitap arsivi
arsiv1
http://bc.vc/HTmutu
veya
http://bc.vc/Ox8uMu
arsiv 2
http://bc.vc/io0GSG
veya
http://bc.vc/f5dgmg
arsıv3
http://bc.vc/OQdXgX
veya
http://bc.vc/skCSFS
arsiv 4
http://bc.vc/JbCIKI
veya
http://bc.vc/F6uHKH
her hersivde birbirinde farklı c++ proglama ilgili kaynaklar mevcutur
kulanmaıcı tafsiye ederim
ücretsiz bu kaynaklardan memlun kalacagınıcı garanti ediyorum
0 new messages