* Load external scm files
@ 2013-12-04 12:37 Lovis Suchmann
2013-12-04 13:52 ` Panicz Maciej Godek
2013-12-04 17:04 ` dsmich
0 siblings, 2 replies; 6+ messages in thread
From: Lovis Suchmann @ 2013-12-04 12:37 UTC (permalink / raw)
To: guile-user
Hello everyone,
I'm working with Guile Scheme (with the LilyPond music engraver
software, if someone knows this), and atm I have the problem that I
would like to load an external scheme file in the current environment, like
; external.scm
(define x 3)
; main.scm
(define (make-test-function)
(load "external.scm")
(lambda () x))
(define test-function (make-test-function))
(display (test-function)) ; => 3
(display x) ; => should create an error because x should not be accessible
I tried but failed with this and found out it is because 'load' always
uses the top-level environment. Is there another way to load a file in
the current context? Like, in the way as if I just copy-and-pasted the
contents of the file at this exact position? Or do you have any other
suggestions?
Thanks in advance. :)
Lovis
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Load external scm files
2013-12-04 13:52 ` Panicz Maciej Godek
@ 2013-12-04 13:48 ` Ralf Mattes
2013-12-04 14:09 ` Panicz Maciej Godek
0 siblings, 1 reply; 6+ messages in thread
From: Ralf Mattes @ 2013-12-04 13:48 UTC (permalink / raw)
To: Panicz Maciej Godek; +Cc: guile-user@gnu.org
On Wed, Dec 04, 2013 at 02:52:10PM +0100, Panicz Maciej Godek wrote:
> This looks like a job for a macro!
>
> (define-macro (define-test-function)
> `(define (test-function)
> ,(load "external.scm")
> x))
>
> (define-test-function)
>
> (test-function)
> ; ==> 3
No - this doesn't solve the OP's question.
Even with your macro, x is still defined in
the toplevel. Try this:
(display (test-function))
(display x) ; visible from the toplevel
(set! x 42)
(display (test-function)) ; Outsch!
Cheers, Ralf Mattes
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Load external scm files
2013-12-04 12:37 Load external scm files Lovis Suchmann
@ 2013-12-04 13:52 ` Panicz Maciej Godek
2013-12-04 13:48 ` Ralf Mattes
2013-12-04 17:04 ` dsmich
1 sibling, 1 reply; 6+ messages in thread
From: Panicz Maciej Godek @ 2013-12-04 13:52 UTC (permalink / raw)
To: Lovis Suchmann; +Cc: guile-user@gnu.org
[-- Attachment #1: Type: text/plain, Size: 186 bytes --]
This looks like a job for a macro!
(define-macro (define-test-function)
`(define (test-function)
,(load "external.scm")
x))
(define-test-function)
(test-function)
; ==> 3
[-- Attachment #2: Type: text/html, Size: 361 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Load external scm files
2013-12-04 13:48 ` Ralf Mattes
@ 2013-12-04 14:09 ` Panicz Maciej Godek
0 siblings, 0 replies; 6+ messages in thread
From: Panicz Maciej Godek @ 2013-12-04 14:09 UTC (permalink / raw)
To: Ralf Mattes; +Cc: guile-user@gnu.org
[-- Attachment #1: Type: text/plain, Size: 351 bytes --]
Oh yes, you're right -- it's because of the behaviour of "load".
However, this should solve the problem:
(define-macro (define-test-function)
`(define (test-function)
,(with-input-from-file "external.scm" read)
x))
(note however that it would load only the first sexp from
"external.scm", but I think that it should be a sufficient hint)
[-- Attachment #2: Type: text/html, Size: 525 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Load external scm files
2013-12-04 12:37 Load external scm files Lovis Suchmann
2013-12-04 13:52 ` Panicz Maciej Godek
@ 2013-12-04 17:04 ` dsmich
2013-12-04 17:37 ` Ralf Mattes
1 sibling, 1 reply; 6+ messages in thread
From: dsmich @ 2013-12-04 17:04 UTC (permalink / raw)
To: Lovis Suchmann, guile-user
---- Lovis Suchmann <panlovis@aol.com> wrote:
> Hello everyone,
>
> I'm working with Guile Scheme (with the LilyPond music engraver
> software, if someone knows this), and atm I have the problem that I
> would like to load an external scheme file in the current environment, like
>
> ; external.scm
> (define x 3)
>
> ; main.scm
> (define (make-test-function)
> (load "external.scm")
> (lambda () x))
> (define test-function (make-test-function))
> (display (test-function)) ; => 3
> (display x) ; => should create an error because x should not be accessible
>
> I tried but failed with this and found out it is because 'load' always
> uses the top-level environment. Is there another way to load a file in
> the current context? Like, in the way as if I just copy-and-pasted the
> contents of the file at this exact position? Or do you have any other
> suggestions?
Sounds like a job for include: http://www.gnu.org/software/guile/manual/html_node/Local-Inclusion.html#index-include
-Dale
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Load external scm files
2013-12-04 17:04 ` dsmich
@ 2013-12-04 17:37 ` Ralf Mattes
0 siblings, 0 replies; 6+ messages in thread
From: Ralf Mattes @ 2013-12-04 17:37 UTC (permalink / raw)
To: dsmich; +Cc: guile-user
On Wed, Dec 04, 2013 at 12:04:34PM -0500, dsmich@roadrunner.com wrote:
>
> Sounds like a job for include: http://www.gnu.org/software/guile/manual/html_node/Local-Inclusion.html#index-include
>
Yes, except Lilypond's main branch strill uses guile-1.8 and that doesn't know 'include.
I think at this point it might be a good idea to ask the OP what problem he tries to solve.
Writing scheme code into a file that gets spliced into a function definition sounds like
a kludge to me. If visibility/encapsulation is the goal, maybe a module is the better
solution (plus, maybe some syntactic sugar to hide guile's module system: (define-plugin ...)
which would expand into (define-module ...) and (use-plugin ...) that would expand into
a (use-modules ...) with an appropriate symbol-renamer that transforms a module name
(foo bar baz) into foo.bar.baz.exported-symbol ...)
Cheers RalfD
> -Dale
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2013-12-04 17:37 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-04 12:37 Load external scm files Lovis Suchmann
2013-12-04 13:52 ` Panicz Maciej Godek
2013-12-04 13:48 ` Ralf Mattes
2013-12-04 14:09 ` Panicz Maciej Godek
2013-12-04 17:04 ` dsmich
2013-12-04 17:37 ` Ralf Mattes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).