[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Placement of defines
From: |
Sebastian Tennant |
Subject: |
Re: Placement of defines |
Date: |
Thu, 18 Dec 2008 18:47:05 +0000 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux) |
Quoth "Kjetil S. Matheussen" <address@hidden>:
> Yes, this limitation in Scheme is a horrible pest. There's no
> decent reason why you shouldn't be allowed to use define other
> places than right after a lambda, except the long discussion
> involved how it should behave in various situations. But even
> inconsistant behaviour between implementations would be better
> than the current situation where you either have to rewrite
> perfectly fine expressions or use various types of lets or
> lambdas in more or less ridiculous ways:
>
> - ((lambda (x) (set! x "baz\n") (define bar x) (display bar)) "foo\n")
> + ((lambda (x) (set! x "baz\n") (let () (define bar x) (display bar) "foo\n"))
Your 'let' workaround gave me an idea.
I use this macro to bind lots of variables dynamically within my
scripts by wrapping calls to it in a map procedure:
(define-macro (definer var val) ;var must be a symbol
`(module-define! (current-module) ,var ,val))
and it happens to work fine as a workaround for the restriction on
define placements in lambdas and lets :)
guile> ((lambda (x) (set! x "baz\n") (definer 'bar x) (display bar)) "foo\n")
baz
guile>
It (the macro) won't work properly if you need/want to compile your
scheme code though.
If that's the case you should use this macro instead:
(define-macro (definer var val) ;var must be a string
`(define ,(string->symbol var) ,val))
I prefer the former macro definition because I don't need to compile my
code and
(definer "foo" "bar")
seems somehow uglier and less intuitive than:
(definer 'foo "bar")
to me.
Incidentally, perhaps you can tell me why the first argument to this
compile-friendly macro has to be a string for it to work.
guile> (define-macro (definer var val)
`(define ,var ,val))
guile> (definer 'foo "bar")
appears to have worked, but...
guile> foo
ERROR: Unbound variable: foo
ABORT: (unbound-variable)
it hasn't.
Sebastian