Error in conditional (cond …) script-fu

2019-09-06 14:10发布

问题:

I'm trying to do a script-fu and I'm using a cond statement theoretically correct, but it always gives the error "Error: ( : 1) illegal function ".

This is the code:

(define (script-fu-prueba 
        edicionInteractiva) 
    (let* 
        (
            (cond 
                ( (equal? edicionInteractiva "Interactivo") (edicionInteractiva RUN-INTERACTIVE) )
                ( (equal? edicionInteractiva "No interactivo") (edicionInteractiva RUN-NONINTERACTIVE) )
            )
        )
    )
)

(script-fu-register "script-fu-prueba" 
    "<Image>/Filters/PRUEBA"
    "Prueba"
    "Author"
    "Copyright"
    "Date"
    ""

    SF-OPTION   "Interactive" '("Interactivo" "No interactivo")
)

What error is there?

I also want to make a conditional statement with multiple statements in both affirmative and negative cases.

Thanks for helping.

回答1:

The script-fu interpreter thinks you are using cond as a variable and trying to initialize it with some sequence of misformed function calls. It doesn't appear that you need the let* syntactic form; its syntax is (let ((<name1> <init1>) ...) body1 body2 ...). Notice that your code makes cond appear as name.

Also, don't forget that cond is an expression; similar to the C language <pred> ? <conseqeuent> : <alternate>. You could thus distill your code to this:

(define (script-fu-prueba edicionInteractiva) 
  (edicionInteractiva (cond ((equal? edicionInteractiva "Interactivo")    RUN-INTERACTIVE)
                            ((equal? edicionInteractiva "No interactivo") RUN-NONINTERACTIVE)
                            (else (error "edicionInteractiva unknown")))))

Edit: As Óscar López notes, your use of edicionInteractiva is inconsistent; apparently a string or a function, can't be both.



回答2:

For starters, the code shown does not follow good Lisp indentation conventions. You must not close parentheses in individual lines, they're not like curly brackets in a C-like language! Also, that let* is completely unnecessary, you're not declaring variables in it. You should use a good IDE or text editor with syntax coloring that also helps you balance the parentheses, otherwise syntax errors will be difficult to catch.

And there's a more serious problem lurking. The parameter (which appears to be a string) is called edicionInteractiva, but that's also the name of the function you want to call - that won't work, they must have different names. I renamed the parameter to modo. I believe you meant this, and notice the correct indentation and the proper way to handle unknown inputs:

(define (script-fu-prueba modo)
  (cond ((equal? modo "Interactivo")
         (edicionInteractiva RUN-INTERACTIVE))
        ((equal? modo "No interactivo")
         (edicionInteractiva RUN-NONINTERACTIVE))
        (else
         ; it's a good idea to add some error handling
         (error "Modo de edición desconocido" modo))))