You are here

dynamicaly defined function - is it posible?

Hello.

I am relatively newbie in scheme programing.

I need define function with dynamicaly defined body. Something like this:
(define (script-fu-eval)
(let* ( (a "it works")
(str (quote (display a)))
)
(eval str)
)
)

but I need define "str" outside of function an pass it as a parameter. It is no problem to this point. The point is: I need construct "str" as a string, convert it and execute it.

Is there some function which parameter is string and ret_val is string without doublequotes?

(funct "(list 1 2)") -> (list 1 2)

Many thanx

Beno

(define (funct str)
  (let* ((p (open-input-string str))
         (obj (read p)) )
    (close-input-port p)
    (eval obj (current-environment)) ))

Surely there must be some other way to do what you want to do. In 99% of the cases, eval is a very bad idea. You probably can pass the function itself as the car of the list and do (apply (car lst) (cdr lst)).

>Is there some function which parameter is string and ret_val is string without doublequotes?

(read (open-input-string "(list 1 2)"))

Again, if you're writing something like this then your code is probably not very efficient.

Many thanx to both.
Ono more question. open-input-string is not (IMHO) a valid R5RS command, its SRFI, but works. I thougth GIMP is R5RS compatibile. What else work? Or: "Try, maybe works"?

This is supposed to be the list of functions available in TinyScheme: http://tinyscheme.sourceforge.net/tinyscm.txt

But it's not even exhaustive... For example get-output-string exists to complement open-output-string, but it isn't mentioned there.

Another interesting one is re-match, which allows to use regular expressions. Here is the description of some extra stuff that is available: http://www.ve3syb.ca/wiki/doku.php?id=software:sf:writing

Also, instead of Scheme-style hygienic macros, TinyScheme uses Common Lisp-style macros, which are IMO easier to understand: http://www.kerosenecow.net/blog/archive/2011/11/Script_fu_Tinyscheme_Mac...

Subscribe to Comments for "dynamicaly defined function - is it posible?"