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

Re: Calling lisp functions

27 views
Skip to first unread message
Message has been deleted

Pascal J. Bourguignon

unread,
Feb 23, 2009, 10:56:15 AM2/23/09
to
Francogrex <fra...@grex.org> writes:

> I'm using ECL a lot but have yet to see posted a comprehensive example
> on how to embed CL functions. I see bits and pieces but nothing
> complete from A to Z. Documentation is not the strongest point of this
> implementation (but sure everything else is, it's an awesome CL
> implementation). Say I have this horrible toy lisp function in file
> toy.lisp:
>
> (defun random-10 () (loop repeat 10 collect (random 1.0) into rndtemp
> finally (return (values (setf rnd rndtemp)))))
>
> and I want to refer to it from within this test.c:
>
> int main ()
> {
> ...
> bla bla bla;
> ...(random-10)...;
> bla bla;
> ...
> }
>
> How to do it and also how to compile it? (what gcc instructions and
> arguments need to be supplied? against which library it needs to be
> linked? on what is it dependent etc?) Either Linux or Win32. Thanks

The following is not tested, I gathered it from an old web page.
That's what I'd try first:


#include "ecl/ecl.h"

int main(int narg, char **argv)
{
cl_boot(narg, argv);
cl_object read_from_string = c_string_to_object("READ_FROM_STRING");
cl_object form=cl_list(2, read_from_string, MAKE_STRING("(random-10)"));
cl_eval(form);
return 0;
}

I'd try: gcc -o example example.c -lecl
to compile it.


--
__Pascal Bourguignon__

Unknown

unread,
Feb 23, 2009, 11:40:48 AM2/23/09
to
Francogrex wrote:

> (defun random-10 () (loop repeat 10 collect (random 1.0) into
> rndtemp finally (return (values (setf rnd rndtemp)))))

Ruby:

def random_10
Array.new(10){ rand }
end


Clojure:

(defn random-10 []
(for (_ (range 10)) (rand)))

Marco Antoniotti

unread,
Feb 23, 2009, 12:06:17 PM2/23/09
to


Call them from C.

Cheers
--
Marco

DanL

unread,
Feb 23, 2009, 1:44:01 PM2/23/09
to
On Feb 23, 5:40 pm, "William James" <> wrote:
> Clojure:
>
> (defn random-10 []
>   (for (_ (range 10)) (rand)))

(defun random-10 ()
(mapping ((_ (scan-range :below 10))) (random 1.0)))

(defun random-10 ()
(subseries (mapping () (random 1.0)) 0 10))

Pascal J. Bourguignon

unread,
Feb 23, 2009, 2:36:24 PM2/23/09
to
p...@informatimago.com (Pascal J. Bourguignon) writes:

> Francogrex <fra...@grex.org> writes:
>
>> I'm using ECL a lot but have yet to see posted a comprehensive example
>> on how to embed CL functions. I see bits and pieces but nothing
>> complete from A to Z. Documentation is not the strongest point of this
>> implementation (but sure everything else is, it's an awesome CL
>> implementation). Say I have this horrible toy lisp function in file
>> toy.lisp:
>>
>> (defun random-10 () (loop repeat 10 collect (random 1.0) into rndtemp
>> finally (return (values (setf rnd rndtemp)))))
>>
>> and I want to refer to it from within this test.c:
>>
>> int main ()
>> {
>> ...
>> bla bla bla;
>> ...(random-10)...;
>> bla bla;
>> ...
>> }
>>
>> How to do it and also how to compile it? (what gcc instructions and
>> arguments need to be supplied? against which library it needs to be
>> linked? on what is it dependent etc?) Either Linux or Win32. Thanks
>
> The following is not tested, I gathered it from an old web page.
> That's what I'd try first:

And here is what works once debugged:

#include "ecl/ecl.h"
int main(int narg, char **argv)
{
cl_boot(narg, argv);

cl_object form=cl_list(2,
c_string_to_object("PRINT"),
cl_list(2,
c_string_to_object("READ-FROM-STRING"),
c_string_to_object("\"(a \\\"b\\\" #xc)\"")));
cl_eval(form);
cl_eval(cl_list(1,c_string_to_object("TERPRI")));
return 0;
}

/*
gcc -I/opt/local/include -I/opt/local/lib/ecl/ -L/opt/local/lib/ecl/ \
-o ecl-test ecl-test.c -lecl \
&& ./ecl-test
*/


(adjust the paths for your system). This program prints:

% ./ecl-test

(A "b" 12)


--
__Pascal Bourguignon__

ayr...@gmail.com

unread,
Feb 23, 2009, 4:24:35 PM2/23/09
to
Francogrex wrote:
> I'm using ECL a lot but have yet to see posted a comprehensive example
> on how to embed CL functions.

I've put up http://github.com/ayrnieu/ecl-examples/tree/master ;
you can

git clone git://github.com/ayrnieu/ecl-examples.git
cd ecl-examples
make

Message has been deleted
Message has been deleted

William James

unread,
Feb 23, 2009, 7:50:48 PM2/23/09
to
William James wrote:

(defn random-10 []
(take 10 (repeatedly rand)))

Message has been deleted

Marco Antoniotti

unread,
Feb 24, 2009, 2:51:29 AM2/24/09
to

(defun rantom-10 () (lazy-seq:take 10 (lazy:repeatedly 'random)))

Which, let's not forget, I can call from C in ECL etc etc. We are
still waiting for the semantically equivalent Ruby version.... :)

But wait! Since you got as far as Clojure, maybe you can start
posting actual Communist Lisp (not that that will save you from the
request of posting the Ruby lazy calling library.... :) )

Cheers
--
Marco

ayr...@gmail.com

unread,
Feb 24, 2009, 4:35:42 AM2/24/09
to
On Feb 24, 1:33 am, Francogrex <fra...@grex.org> wrote:
> I don't think any element in this list can be used with C functions.
> For example would you be able to take the (nth 1 rnd) and do a C
> printf on it or use it in other C arithmetic operations? -like (nth 1
> rnd)+7 - I doesn'ty seem to be.

void randtest (int index) {
cl_object list;
list = cl_eval(READCL((loop repeat 10 collect (random 1.0))));
cl_print(1, list);
cl_terpri(0);
printf("random: %f\n", ecl_to_float(cl_nth(MAKE_FIXNUM(index),
list)));
}

Added to that ecl-examples repo.

Message has been deleted
Message has been deleted
Message has been deleted

Pascal J. Bourguignon

unread,
Feb 24, 2009, 11:05:40 PM2/24/09
to
Francogrex <fra...@grex.org> writes:

> On Feb 24, 10:35 am, ayrn...@gmail.com wrote:
>> void randtest (int index) {
>>   cl_object list;
>>   list = cl_eval(READCL((loop repeat 10 collect (random 1.0))));
>>   cl_print(1, list);
>>   cl_terpri(0);
>>   printf("random: %f\n", ecl_to_float(cl_nth(MAKE_FIXNUM(index),
>> list)));
>>
>> }
>

> May I ask where did you learn those, where did you get the
> information? Because on the ecl sourceforge site the current manual is
> very laconic with minimal details and it doesn't show satisfactory
> examples like those above?

Have a look at ecl.h and its brother files.
It may be installed in /usr/include/ecl/ecl.h
or /usr/lib/ecl/ecl/ecl.h
or somewhere else.

--
__Pascal Bourguignon__

Message has been deleted

ayr...@gmail.com

unread,
Mar 1, 2009, 10:49:13 PM3/1/09
to
On Mar 1, 5:23 pm, Francogrex <fra...@grex.org> wrote:
> Hi what's the [...] MAKE_FIXNUM to pass a float or double from
> C to ECL.
> Is there sometyhing like MAKE_FLOAT?

void floats (void) {
cl_object vector;
vector = cl_vector(2, ecl_make_singlefloat(1234567890123456.0),
ecl_make_doublefloat(1234567890123456.0));
cl_print(1, vector);
cl_terpri(0);
}

If you've installed ECL into /usr/local , you can look through the
headers in /usr/local/include/ecl/ [the way I did to answer your
question].

0 new messages