Question

I never used any other implementations than GIMP's script-fu, and I am even quite new to GIMP's Scheme, too. So, maybe I'm wrong. However, the followings do not work in Script-Fu console:

(define x 13)
(define s 'x) ; or (define s `x)
,s => Error: eval: unbound variable: unquote 

similarly

(unquote s) => Error: eval: unbound variable: unquote 

It seems to me, that "," was planned to work but unquote has not been implemented. If so, how can I solve the following problem?

(define x 13)
(define y 7)
; define procedure to swap x and y
(define (swap) 
  (let ((t 0))
    (set! t ,'x)
    (set! x ,'y)
    (set! y t)
  )
)

This should run multiple times, so (set! t x)... will not work.

Was it helpful?

Solution

Your swap function will work as:

(define (swap)
  (let ((t 0))
    (set! t x)
    (set! x y)
    (set! y t)))

because set! is a syntactic keyword that evaluates its arguments specially. The first argument (t in the first set! expression in swap above) is not evaluated; rather it is used to identify a location. The second argument (x) is evaluated and, in this case, returns the value held in the location referenced by the identifier (x).

The syntactic keyword define works similarly. The first argument is not evaluated; it is used to create a new location; the second argument is evaluated. So, for your example, unquote is not needed just use:

(define s x)

If you are trying to define s so it returns the current value of x rather then the value bound to x when s is allocated, then make s a function:

(define (s) x)

Then the expression (s) will return whatever x is currently bound to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top