> 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__
> (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)))
Call them from C.
Cheers
--
Marco
(defun random-10 ()
(mapping ((_ (scan-range :below 10))) (random 1.0)))
(defun random-10 ()
(subseries (mapping () (random 1.0)) 0 10))
> 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__
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
(defn random-10 []
(take 10 (repeatedly rand)))
(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
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.
> 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__
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].