|
From: | Damien Mattei |
Subject: | Re: new function |
Date: | Wed, 22 Sep 2021 10:44:48 +0200 |
Preview:
(define-syntax <- (syntax-rules () ((_ var expr) (let* ((modul (current-module)) (exist-var (module-variable modul (quote var)))) (if exist-var (module-set! modul (quote var) expr) (module-define! modul (quote var) expr))))))
notice that the <- operator defined above has been removed of the arrays,vectors,etc support for readability, here i focus only on "simple" scheme variable ,arrays,vectors is working great already.
Preview:
(define (subset-sum-guile L t) {ls <- (length L)} {dyn <- dyna[ls t]} ;; dyna[ls][t] means 0: unknown solution, 1: solution found, 2: no solution (condx [{dyn <> 0} (one? dyn)] [(null? L) {dyna[ls t] <- 2} #f] ;; return #f [exec {c <- (first L)}] ;; c is the solution [{c = t} {dyna[ls t] <- 1} #t] ;; return #t [exec {R <- (rest L)}] ;; continue searching a solution in the rest [{c > t} {s <- (subset-sum-guile R t)} {dyna[ls t] <- (one-two s)} s] ;; return boolean value ;; else : c < t at this point ;; c is part of a solution OR not part of a solution [else {s <- {(subset-sum-guile R {t - c}) or (subset-sum-guile R t)}} {dyna[ls t] <- (one-two s)} s])) ;; return boolean value
On 19.09.2021 09:54, Damien Mattei wrote:
> hello,
> i'm developing an extension to Scheme
> and i need a procedure or macro that define a variable only if it is not bind and if it is just set! it.
>
> I can not do it in Guile or any Scheme,and i'm desperately searching a way to do that. I finally conclude that it can be done only by adding it in the language.
>
> Can someone include a such function in Guile next release?
> i know guile have a predicate defined? to test binfing of a vairable but writing a macro with it is not possible because define can be used in an _expression_ context.
>
> Thank in advance for any help
> Damien
What is the utility of such a function? Since Scheme is lexically scoped,
it's generally obvious whether a variable has already been defined or not,
rendering such an operation useless.
If you're constructing code from outside input so that you don't know
whether a provided variable name represents a variable that's already been
defined or not, then you could use the module reflection API:
https://www.gnu.org/software/guile/manual/html_node/Module-System-Reflection.html
Sounds like 'module-define!' does exactly what you ask for. I wonder what
exactly your use-case is though. Chances are that your use-case is best
served with a simple data structure like a hash table...
--
Taylan
[Prev in Thread] | Current Thread | [Next in Thread] |