cond(itionals) with optional execution of statements

2021-09-11 Thread Damien Mattei
hello,

i wrote a little macro (file condx.scm)  that allow :  cond(itionals) with
optional execution of statements before:

(define-syntax condx
  (syntax-rules (exec)
((_)
 (error 'condx "No else clause"))
((_ (else e ...))
 (let () e ...))
((_ (exec s ...) d1 ...)
 (let () s ... (condx d1 ...)))
((_ (t e ...) tail ...)
 (if t
 (let () e ...)
 (condx tail ...)

use it like that:

mattei@macbook-pro-touch-bar library-FunctProg % guile
GNU Guile 3.0.7
Copyright (C) 1995-2021 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
scheme@(guile-user)> (load "condx.scm")
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;   or pass the --no-auto-compile argument to disable.
;;; compiling /Users/mattei/Dropbox/git/library-FunctProg/condx.scm
;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.5/Users/mattei/Dropbox/git/library-FunctProg/condx.scm.go
scheme@(guile-user)> (define x 1)

(condx ((= x 7) 'never)
(exec
  (define y 3)
  (set! x 7))
((= y 1) 'definitely_not)
(exec
  (set! y 10)
  (define z 2))
((= x 7) (+ x y z))
(else 'you_should_not_be_here))
$1 = 19

i share it to have idea about critics or idea to improve it as it will be
part of  a Scheme extension to scheme language that will include other
features

have a good day

Damien


condx.scm
Description: Binary data


Re: Trouble creating SRFI-9 Record in C

2021-09-11 Thread Matt Wette

maybe add

(define (make-foo-x a b) (make-foo a b))

then call make-foo-x (or reverse names)

On 9/10/21 7:27 PM, paul wrote:

Good day guile-users,

I am having a struggle with SRFI-9 records.  They look very 
convenient, so i'd like to use them in my Guile scripts.  However, i'm 
not sure how to correctly construct them from C-land.  I have 
something like the following:


```
(define-record-type 
 (make-foo a b)
 foo?
 (a foo-a)
 (b foo-b))
```

In Guile land, that works great.  Now, i want to create a foo in C and 
pass it to a function in the Guile script.  I do something like the 
following:


```
scm_c_primitive_load("foo.scm");
scm_call_5(scm_variable_ref(scm_c_lookup("make-foo")),
  scm_from_utf8_string("blah"),
  scm_from_int32(Int32(42)))
```

However, this results in an error:

guile: uncaught exception:
Wrong type to apply: #

I've tried with and without (define-module foo) at the top of the 
file, that doesn't seem to make a difference.  I've been able to work 
around the issue by defining a wrapper (define (foo-prime a b) 
(make-foo a b)) and using that in C as shown above, but that feels 
ugly.  I'm probably missing something obvious, but trawling the 
mailing list didn't turn up anything i could understand.


Does anyone see what i'm doing wrong, or can i simply not use SRFI-9 
records in this way?


Thanks, 🙌
p.






Re: Trouble creating SRFI-9 Record in C

2021-09-11 Thread paul
Hey Matt,

Yeah that was exactly my workaround 🙂 I was wondering whether i could use the 
syntax transformer more "directly" from C, or something like that. 

Thanks,
paul

> On 11 Sep 2021, at 23:31, Matt Wette  wrote:
> 
> maybe add
> 
> (define (make-foo-x a b) (make-foo a b))
> 
> then call make-foo-x (or reverse names)
> 
>> On 9/10/21 7:27 PM, paul wrote:
>> Good day guile-users,
>> 
>> I am having a struggle with SRFI-9 records.  They look very convenient, so 
>> i'd like to use them in my Guile scripts.  However, i'm not sure how to 
>> correctly construct them from C-land.  I have something like the following:
>> 
>> ```
>> (define-record-type 
>>  (make-foo a b)
>>  foo?
>>  (a foo-a)
>>  (b foo-b))
>> ```
>> 
>> In Guile land, that works great.  Now, i want to create a foo in C and pass 
>> it to a function in the Guile script.  I do something like the following:
>> 
>> ```
>> scm_c_primitive_load("foo.scm");
>> scm_call_5(scm_variable_ref(scm_c_lookup("make-foo")),
>>   scm_from_utf8_string("blah"),
>>   scm_from_int32(Int32(42)))
>> ```
>> 
>> However, this results in an error:
>> 
>> guile: uncaught exception:
>> Wrong type to apply: #
>> 
>> I've tried with and without (define-module foo) at the top of the file, that 
>> doesn't seem to make a difference.  I've been able to work around the issue 
>> by defining a wrapper (define (foo-prime a b) (make-foo a b)) and using that 
>> in C as shown above, but that feels ugly.  I'm probably missing something 
>> obvious, but trawling the mailing list didn't turn up anything i could 
>> understand.
>> 
>> Does anyone see what i'm doing wrong, or can i simply not use SRFI-9 records 
>> in this way?
>> 
>> Thanks, 🙌
>> p.
>> 
> 
>