2

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.

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
FERcsI
  • 388
  • 1
  • 10
  • `unquote` doesn't work the way you think it does, in any implementation, ever. Also, I don't understand what you mean by "`(set! t x)` will not work". – C. K. Young Oct 07 '13 at 12:26
  • In particular, [`unquote` is not `eval`](http://stackoverflow.com/q/18515295/13), and you shouldn't use `eval` for this, either. – C. K. Young Oct 07 '13 at 12:29
  • I tried `(set! t x)` first, but it did not work. It swapped the values just once and never again. I thought values x and y have been evaluated in "define-time". Now I tried it again, and works. I do not know, what I made wrong. Thanks. – FERcsI Oct 08 '13 at 07:24

1 Answers1

0

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.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • That was the first thing I tried, but it did not work (that is what I wrote in my post). It swapped the values just once and never again. I thought values x and y have been evaluated in "define-time". Now I tried it again, and works. I do not know, what I made wrong. Thanks. – FERcsI Oct 08 '13 at 07:23