Hello Mortimer,
i get in a lot of trouble doing those sort of things with Scheme.
i mean trying like in C , expecting to have exported variable , in a object
file , and thinking that they are fixed at an address in memory, even this
idea is just an assumption of how i expected things to work. But this does
not seem to be the case with scheme module, and that is not special to
Guile.
i test your code and get a different result, so i suppose, it is normal
(not specified to work) or a bug:
scheme@(guile-user)> (load "testexport.scm")
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /Users/mattei/Scheme-PLUS-for-Guile/testexport.scm
;;; compiling /usr/local/share/guile/site/3.0/env.scm
;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/usr/local/share/guile/site/3.0/env.scm.go
"varB post cond: B"
"varC post if: C"
;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/Users/mattei/Scheme-PLUS-for-Guile/testexport.scm.go
"varA in main: A"
"varB in main: "
"varC in main: "

if you want to do that, it is better to do not export the variable
themselves but some accessors, or perheaps use the GOOPS ,object system of
Guile.

with accessors this works for me, and i hope you too:

(define-module (env)
  #:use-module (ice-9 pretty-print)
  #:export(varA getB getC ))

(define varA "A")
(define varB "")
(define varC "")
(define testval "x")

(define (getB)
  varB)

(define (getC)
  varC)


(cond
 ((string= testval "x") (set! varB "B"))
 ((string= testval "y") (set! varB "error"))
 ((string= testval "z") (set! varB "null"))
 )
(pretty-print (string-append "varB post cond: " varB))

(if (string= testval "x")  (set! varC "C"))
(pretty-print (string-append "varC post if: " varC))

(define-module (testexport)
  #:use-module (ice-9 pretty-print)
  #:use-module (env)
  )

(define (main)
  (pretty-print (string-append  "varA in main: " varA))
  ;;(pretty-print (string-append  "varB in main: " varB))
  ;;(pretty-print (string-append  "varC in main: " varC))

  (pretty-print (string-append  "varA in main: " varA))
  (pretty-print (string-append  "varB in main: " (getB)))
  (pretty-print (string-append  "varC in main: " (getC)))
  )

(main)

and the result is ok:

scheme@(guile-user)> (load "testexport.scm")
;;; note: source file /Users/mattei/Scheme-PLUS-for-Guile/testexport.scm
;;;       newer than compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/Users/mattei/Scheme-PLUS-for-Guile/testexport.scm.go
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the --no-auto-compile argument to disable.
;;; compiling /Users/mattei/Scheme-PLUS-for-Guile/testexport.scm
;;; note: source file /usr/local/share/guile/site/3.0/env.scm
;;;       newer than compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/usr/local/share/guile/site/3.0/env.scm.go
;;; compiling /usr/local/share/guile/site/3.0/env.scm
;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/usr/local/share/guile/site/3.0/env.scm.go
"varB post cond: B"
"varC post if: C"
;;; compiled
/Users/mattei/.cache/guile/ccache/3.0-LE-8-4.6/Users/mattei/Scheme-PLUS-for-Guile/testexport.scm.go
"varA in main: A"
"varA in main: A"
"varB in main: B"
"varC in main: C"

Best Regards,

Damien

On Tue, Jan 23, 2024 at 2:38 PM Mortimer Cladwell <mbcladw...@gmail.com>
wrote:

> Hi,
>
> Suppose I have the following module "testexport" with the auxiliary module
> "env.scm", used to set global environment variables:
>
> -------------testexport.scm begin----------------------------------
> (define-module (testexport)
>   #:use-module (ice-9 pretty-print)
>   #:use-module (env)
>   )
>
> (define (main)
>   (pretty-print (string-append  "varA in main: " varA))
>   (pretty-print (string-append  "varB in main: " varB))
>   (pretty-print (string-append  "varC in main: " varC))
>   )
>
> (main)
> -------------testexport.scm end----------------------------------
>
> -------------env.scm begin----------------------------------
> (define-module (env)
>   #:use-module (ice-9 pretty-print)
>   #:export(varA varB varC))
>
> (define varA "A")
> (define varB "")
> (define varC "")
> (define testval "x")
>
> (cond
>  ((string= testval "x") (set! varB "B"))
>  ((string= testval "y") (set! varB "error"))
>  ((string= testval "z") (set! varB "null"))
>  )
> (pretty-print (string-append "varB post cond: " varB))
>
> (if (string= testval "x")  (set! varC "C"))
> (pretty-print (string-append "varC post if: " varC))
> -------------env.scm end----------------------------------
>
> When I run the above I get as output:
>
> "varB post cond: B"
> "varC post if: C"
> "varA in main: A"
> "varB in main: "
> "varC in main: C"
>
> Why can I reset the variables with both 'cond' and 'if' in env.scm, but
> only the variable reset with 'if' is exported with the updated value?
> Thanks
> Mortimer
>

Reply via email to