[Chicken-users] help :)

2013-06-05 Thread nehal singhal
Hi,
   I am a newbie to chicken.Can i get some aid as to how to start
coding through chicken. I was recently learning racket and also have
know-how of Python-2.6.
   Please guide a little.

regards,
Nehal Singhal.

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] set! atomic?

2013-06-05 Thread Felix
From: Bryan Vicknair bryanv...@gmail.com
Subject: [Chicken-users] set! atomic?
Date: Tue, 4 Jun 2013 19:15:52 -0700

 SRFI-18 states:
 
   Read and write operations on the store (such as reading and writing a
   variable, an element of a vector or a string) are not required to be atomic.
   It is an error for a thread to write a location in the store while some 
 other
   thread reads or writes that same location.
 
 Is it possible to eval a variable that is in an inconsistent state?  I
 understand that if there are a series of set-car! on a list, then a reader may
 see the list as it is in between any of the set-car! calls.  But is it 
 possible
 that an update to a very large object will ever be interrupted by one thread
 such that other threads will see a broken version?
 
   (use srfi-1)
   (define foo '(1 2 3))
   (thread-start! (make-thread (lambda () (set! foo (iota 1e8)
   (print foo)
 
 In the above code, will the primordial thread ever print a list that
 isn't exactly (1 2 3) or (iota 1e8)?

The assignment itself is fully atomic, as is the destructive
modification of any single data-cell like pair-cells, vector-elements
or record-structure slots.


cheers
felix

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] help :)

2013-06-05 Thread Matt Gushee
Hi, Nehal--

Have you seen the Chicken for Python programmers tutorial?

http://wiki.call-cc.org/chicken-for-python-programmers

That would be a good place to start. Then if you are still unsure how
to proceed, you will probably get more help if you ask more specific
questions.
Best of luck with Chicken!

--
Matt Gushee

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] help :)

2013-06-05 Thread Mario Domenech Goulart
Hi,

On Wed, 5 Jun 2013 16:07:56 +0200 nehal singhal nehalsingha...@gmail.com 
wrote:

I am a newbie to chicken.Can i get some aid as to how to start
 coding through chicken. I was recently learning racket and also have
 know-how of Python-2.6.
Please guide a little.

Welcome.

There's the Getting started chapter from the manual:
http://wiki.call-cc.org/man/4/Getting%20started

Best wishes.
Mario
-- 
http://parenteses.org/mario

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


[Chicken-users] Chicken C interface

2013-06-05 Thread pluijzer .
Hello everybody,

I was planning to use Chicken Scheme in a fashion more similar to Guile and
Lua. i.e. passing Scheme Data Objects from Chicken to C and back using the
C interface.

I am a little confused though as how to have a C function return a Scheme
List that is seen by the garbage collector.

For example, is the code below correct, will the list be seen by the
garbage collector? And if not, is there correct way to do this.

#
C_word give_12()
{
  C_word *mem  = C_alloc(C_SIZEOF_LIST(2));
  C_word  list = C_list(mem, 2, C_fix(1), C_fix(2));
  return list;
}
#
(print ((foreign-lambda scheme-object give_12)))

Also there doesn't seem to be a C_listp predicate. Is this a concious
omission?

thank you in advance,
Richard
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] help :)

2013-06-05 Thread Dan Leslie

Feel free to ask questions in the IRC channel, #chicken on irc.freenode.net

-Dan

On 6/5/2013 7:07 AM, nehal singhal wrote:

Hi,
I am a newbie to chicken.Can i get some aid as to how to start
coding through chicken. I was recently learning racket and also have
know-how of Python-2.6.
Please guide a little.

regards,
Nehal Singhal.

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users



___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Dan Leslie

I do this a fair bit in the Allegro egg.

Here's an example:
https://github.com/dleslie/allegro-egg/blob/985ca2ceef0f5b4028af3f97729f13cba2976fe5/color.scm

Basically, use C_alloc to allocate the memory required to host both the 
List structure and the data it is to contain, then use the C_list macro 
to patch it all together.


-Dan

On 6/5/2013 8:10 AM, pluijzer . wrote:

Hello everybody,

I was planning to use Chicken Scheme in a fashion more similar to 
Guile and Lua. i.e. passing Scheme Data Objects from Chicken to C and 
back using the C interface.


I am a little confused though as how to have a C function return a 
Scheme List that is seen by the garbage collector.


For example, is the code below correct, will the list be seen by the 
garbage collector? And if not, is there correct way to do this.


#
C_word give_12()
{
  C_word *mem  = C_alloc(C_SIZEOF_LIST(2));
  C_word  list = C_list(mem, 2, C_fix(1), C_fix(2));
  return list;
}
#
(print ((foreign-lambda scheme-object give_12)))

Also there doesn't seem to be a C_listp predicate. Is this a concious 
omission?


thank you in advance,
Richard


___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] help :)

2013-06-05 Thread Pedro Melendez
Hey Dan,

What's the preferred method to ask? I didn't know about the IRC channel and
now I am dubious what would be better if asking over there or using the
email list...

Cheers,

Pedro.


On Wed, Jun 5, 2013 at 11:44 AM, Dan Leslie d...@ironoxide.ca wrote:

 Feel free to ask questions in the IRC channel, #chicken on
 irc.freenode.net

 -Dan


 On 6/5/2013 7:07 AM, nehal singhal wrote:

 Hi,
 I am a newbie to chicken.Can i get some aid as to how to start
 coding through chicken. I was recently learning racket and also have
 know-how of Python-2.6.
 Please guide a little.

 regards,
 Nehal Singhal.

 __**_
 Chicken-users mailing list
 Chicken-users@nongnu.org
 https://lists.nongnu.org/**mailman/listinfo/chicken-usershttps://lists.nongnu.org/mailman/listinfo/chicken-users



 __**_
 Chicken-users mailing list
 Chicken-users@nongnu.org
 https://lists.nongnu.org/**mailman/listinfo/chicken-usershttps://lists.nongnu.org/mailman/listinfo/chicken-users




-- 
T: +1 (416) - 357.5356
Skype ID: pmelendezu
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Peter Bex
On Wed, Jun 05, 2013 at 08:47:45AM -0700, Dan Leslie wrote:
 I do this a fair bit in the Allegro egg.
 
 Here's an example:
 https://github.com/dleslie/allegro-egg/blob/985ca2ceef0f5b4028af3f97729f13cba2976fe5/color.scm
 
 Basically, use C_alloc to allocate the memory required to host both the 
 List structure and the data it is to contain, then use the C_list macro 
 to patch it all together.

 Also there doesn't seem to be a C_listp predicate. Is this a concious 
 omission?

There is no C_listp predicate because you can't directly check an
object for being a list; you must check whether it's
C_SCHEME_END_OF_LIST (then it is a list).  Otherwise, if it's a pair
you take its cdr and loop.  If it's something else, it's not a list.

Because this is a potentially heavy operation (and cyclic lists may
even cause an endless loop), I guess it's been decided not to provide
a predicate procedure for this, because it's too tricky to get right,
and usually you want to avoid traversing the list twice anyway, so the
operation itself is often embedded in the CDRing logic.

Cheers,
Peter
-- 
http://www.more-magic.net

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Thomas Chust
On 2013-06-05 19:50, Peter Bex wrote:
 [...]
 There is no C_listp predicate because you can't directly check an
 object for being a list; you must check whether it's
 C_SCHEME_END_OF_LIST (then it is a list).  Otherwise, if it's a pair
 you take its cdr and loop.  If it's something else, it's not a list.
 
 Because this is a potentially heavy operation (and cyclic lists may
 even cause an endless loop), I guess it's been decided not to provide
 a predicate procedure for this,
 [...]

Hello,

but it's trivial to detect cyclic lists during the traversal using
either a set of seen elements or just two iteration pointers travelling
at different speeds.

The only problematic structures are truly infinite lists without
periodicity, but those are impossible with eager data structures like
the standard Scheme lists.

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


-- 
When C++ is your hammer, every problem looks like your thumb.


___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Peter Bex
On Wed, Jun 05, 2013 at 07:57:49PM +0200, Thomas Chust wrote:
 Hello,
 
 but it's trivial to detect cyclic lists during the traversal using
 either a set of seen elements or just two iteration pointers travelling
 at different speeds.

In C that's rather painful.  Note that the OP was asking specifically
for a C_listp macro.

Cheers,
Peter
-- 
http://www.more-magic.net

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Thomas Chust
On 2013-06-05 20:11, Peter Bex wrote:
 On Wed, Jun 05, 2013 at 07:57:49PM +0200, Thomas Chust wrote:

 but it's trivial to detect cyclic lists during the traversal using
 either a set of seen elements or just two iteration pointers travelling
 at different speeds.
 
 In C that's rather painful.  Note that the OP was asking specifically
 for a C_listp macro.
 [...]

Hello,

well, I think that two iteration pointers are trivial even in C, but
that may be a matter of taste :-)

Ciao,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.


-- 
When C++ is your hammer, every problem looks like your thumb.


___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread db05

   

this should work for basic list test

(import foreign)

#
C_word C_listp(C_word p)
{
 if (p == C_SCHEME_END_OF_LIST)
 {
 return C_SCHEME_TRUE;
 }

 // check for non-immidiate object and pair?
 if (!C_immediatep(p)  C_pairp(p) ==
C_SCHEME_TRUE)
 {
 return
C_listp(C_u_i_cdr(p));
 }

 return C_SCHEME_FALSE;
}
#

(define listp? (foreign-lambda scheme-object "C_listp" scheme-object))

(print (listp? 1))
(print (listp? (cons 1 2)))
(print (listp? (cons (cons 1 2) (cons 3 4
(print (listp? 1.0))
(print (listp? '(1)))
(print (listp? '(1 2)))

06/05/13 19:10:41, pluijzer . pluij...@gmail.com:
Hello
everybody,

I was planning to use Chicken Scheme in a fashion more similar to
Guile and Lua. i.e. passing Scheme Data Objects from Chicken to C and back
using the C interface.

I am a little confused though as how to have a C function return a
Scheme List that is seen by the garbage collector.

For example, is the code below correct, will the list be seen by the
garbage collector? And if not, is there correct way to do this.

#
C_word give_12()
{
 C_word *mem = C_alloc(C_SIZEOF_LIST(2));
 C_word list = C_list(mem, 2, C_fix(1), C_fix(2));
 return list;
}
#
(print ((foreign-lambda scheme-object "give_12")))

Also there doesn't seem to be a C_listp
predicate. Is this a concious omission?

thank you in advance,
Richard


___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users



___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Peter Bex
On Wed, Jun 05, 2013 at 08:19:15PM +0200, Thomas Chust wrote:
 On 2013-06-05 20:11, Peter Bex wrote:
  On Wed, Jun 05, 2013 at 07:57:49PM +0200, Thomas Chust wrote:
  but it's trivial to detect cyclic lists during the traversal using
  either a set of seen elements or just two iteration pointers travelling
  at different speeds.
  
  In C that's rather painful.  Note that the OP was asking specifically
  for a C_listp macro.
  [...]
 
 Hello,
 
 well, I think that two iteration pointers are trivial even in C, but
 that may be a matter of taste :-)

Of course you're right.  I was thinking of the set of seen elements.
I need sleep.

Cheers,
Peter
-- 
http://www.more-magic.net

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Felix
From: Dan Leslie d...@ironoxide.ca
Subject: Re: [Chicken-users] Chicken C interface
Date: Wed, 05 Jun 2013 08:47:45 -0700

 I do this a fair bit in the Allegro egg.
 
 Here's an example:
 https://github.com/dleslie/allegro-egg/blob/985ca2ceef0f5b4028af3f97729f13cba2976fe5/color.scm
 
 Basically, use C_alloc to allocate the memory required to host both
 the List structure and the data it is to contain, then use the C_list
 macro to patch it all together.

Note that this code is not correct: C_alloc allocates on the C stack and the
data will be invalid once the function returns (Sorry). If this works, then
it is just coincidental!


cheers,
felix

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Felix
From: pluijzer . pluij...@gmail.com
Subject: [Chicken-users] Chicken C interface
Date: Wed, 5 Jun 2013 17:10:41 +0200

 Hello everybody,
 
 I was planning to use Chicken Scheme in a fashion more similar to Guile and
 Lua. i.e. passing Scheme Data Objects from Chicken to C and back using the
 C interface.
 
 I am a little confused though as how to have a C function return a Scheme
 List that is seen by the garbage collector.
 
 For example, is the code below correct, will the list be seen by the
 garbage collector? And if not, is there correct way to do this.
 
 #
 C_word give_12()
 {
   C_word *mem  = C_alloc(C_SIZEOF_LIST(2));
   C_word  list = C_list(mem, 2, C_fix(1), C_fix(2));
   return list;
 }
 #
 (print ((foreign-lambda scheme-object give_12)))

As written in the other message: C_alloc is just a wrapper around alloca(3),
so returning from the function invalidates or overwrites the data. A later
(minor) garbage collection may actually recover the data, but it is not
certain that it happens. See 

  http://api.call-cc.org/doc/foreign/access/foreign-primitive

for a variant that allows stack-allocation.


cheers,
felix

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Dan Leslie

Oh dear!

Well, it works and I haven't had problems. What's the correct way to go 
about this?


-Dan

On 6/5/2013 2:36 PM, Felix wrote:

From: Dan Leslie d...@ironoxide.ca
Subject: Re: [Chicken-users] Chicken C interface
Date: Wed, 05 Jun 2013 08:47:45 -0700


I do this a fair bit in the Allegro egg.

Here's an example:
https://github.com/dleslie/allegro-egg/blob/985ca2ceef0f5b4028af3f97729f13cba2976fe5/color.scm

Basically, use C_alloc to allocate the memory required to host both
the List structure and the data it is to contain, then use the C_list
macro to patch it all together.

Note that this code is not correct: C_alloc allocates on the C stack and the
data will be invalid once the function returns (Sorry). If this works, then
it is just coincidental!


cheers,
felix



___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Dan Leslie

Thanks, I'll get on updating my broken eggs soon.

obvious humpty dumpty joke notwithstanding

-Dan

On 6/5/2013 2:39 PM, Felix wrote:

From: pluijzer . pluij...@gmail.com
Subject: [Chicken-users] Chicken C interface
Date: Wed, 5 Jun 2013 17:10:41 +0200


Hello everybody,

I was planning to use Chicken Scheme in a fashion more similar to Guile and
Lua. i.e. passing Scheme Data Objects from Chicken to C and back using the
C interface.

I am a little confused though as how to have a C function return a Scheme
List that is seen by the garbage collector.

For example, is the code below correct, will the list be seen by the
garbage collector? And if not, is there correct way to do this.

#
C_word give_12()
{
   C_word *mem  = C_alloc(C_SIZEOF_LIST(2));
   C_word  list = C_list(mem, 2, C_fix(1), C_fix(2));
   return list;
}
#
(print ((foreign-lambda scheme-object give_12)))

As written in the other message: C_alloc is just a wrapper around alloca(3),
so returning from the function invalidates or overwrites the data. A later
(minor) garbage collection may actually recover the data, but it is not
certain that it happens. See

   http://api.call-cc.org/doc/foreign/access/foreign-primitive

for a variant that allows stack-allocation.


cheers,
felix

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users



___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Kristian Lein-Mathisen
I just though I'd mention srfi-4 http://api.call-cc.org/doc/srfi-4 as
well, which are much easier to interface with from C. If all your elements
are integers, for example, you might want to check out u32vector. Srfi-4
vectors use plain C float/int arrays and are possible as argument-types
from foreign-lambda and friends.

K.


On Wed, Jun 5, 2013 at 5:10 PM, pluijzer . pluij...@gmail.com wrote:

 Hello everybody,

 I was planning to use Chicken Scheme in a fashion more similar to Guile
 and Lua. i.e. passing Scheme Data Objects from Chicken to C and back using
 the C interface.

 I am a little confused though as how to have a C function return a Scheme
 List that is seen by the garbage collector.

 For example, is the code below correct, will the list be seen by the
 garbage collector? And if not, is there correct way to do this.

 #
 C_word give_12()
 {
   C_word *mem  = C_alloc(C_SIZEOF_LIST(2));
   C_word  list = C_list(mem, 2, C_fix(1), C_fix(2));
   return list;
 }
 #
 (print ((foreign-lambda scheme-object give_12)))

 Also there doesn't seem to be a C_listp predicate. Is this a concious
 omission?

 thank you in advance,
 Richard

 ___
 Chicken-users mailing list
 Chicken-users@nongnu.org
 https://lists.nongnu.org/mailman/listinfo/chicken-users


___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] Chicken C interface

2013-06-05 Thread Ivan Raikov
The correct way is to not let C manage memory at all ;-P
In the mpi egg, I used foreign-primitive and C_alloc as follows:

;; Returns the current MPI time as a floating-point number
(define MPI:wtime
  (foreign-primitive scheme-object ()
#EOF
  C_word result;
  C_word *ptr;

  ptr = C_alloc (C_SIZEOF_FLONUM);

  result = C_number(ptr, MPI_Wtime());

  C_return (result);
EOF
))

In various other C interface libraries, I use the following idiom:

(define (func arg)

  ;; determine the size of the result vector
  (let* ((n (c_func_result_length arg))

;; allocate memory for the result vector
 (v (make-f64vector n 0.0)))

;; obtain result
(c_func arg v)

v))

This of course assumes that the C library you are targeting is structured
so as to allow you
to determine the result size. This is often the case with various numerical
libraries I have had to
interface to, but that's a fairly specific use case.


   -Ivan



On Thu, Jun 6, 2013 at 6:45 AM, Dan Leslie d...@ironoxide.ca wrote:

 Oh dear!

 Well, it works and I haven't had problems. What's the correct way to go
 about this?

 -Dan


 On 6/5/2013 2:36 PM, Felix wrote:

 From: Dan Leslie d...@ironoxide.ca
 Subject: Re: [Chicken-users] Chicken C interface
 Date: Wed, 05 Jun 2013 08:47:45 -0700

  I do this a fair bit in the Allegro egg.

 Here's an example:
 https://github.com/dleslie/**allegro-egg/blob/**
 985ca2ceef0f5b4028af3f97729f13**cba2976fe5/color.scmhttps://github.com/dleslie/allegro-egg/blob/985ca2ceef0f5b4028af3f97729f13cba2976fe5/color.scm

 Basically, use C_alloc to allocate the memory required to host both
 the List structure and the data it is to contain, then use the C_list
 macro to patch it all together.

 Note that this code is not correct: C_alloc allocates on the C stack and
 the
 data will be invalid once the function returns (Sorry). If this works,
 then
 it is just coincidental!


 cheers,
 felix



 __**_
 Chicken-users mailing list
 Chicken-users@nongnu.org
 https://lists.nongnu.org/**mailman/listinfo/chicken-usershttps://lists.nongnu.org/mailman/listinfo/chicken-users

___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users