Mostrando entradas con la etiqueta 03 ESTRUCTURAS DE CONTROL. Mostrar todas las entradas
Mostrando entradas con la etiqueta 03 ESTRUCTURAS DE CONTROL. Mostrar todas las entradas

jueves, 11 de febrero de 2010

3.5. cond

Evalúa distintas condiciones, ejecutando el código que la sigue si se cumple. Su esquema general es:
    (cond 
((condicion1)
(func1 arg1 arg2)
(func2 arg4 arg5)
)
((condicion2)
(func3 arg3 arg4
(func2 arg4 arg5)
)
(else ; si no cumple ninguna de las anteriores
(func4 arg5 arg6)
)
)


Ejemplos:
    (target_red
(cond
((= mode 0) average)
((= mode 1) 255)
((= mode 2) (car bg))
)
)


Esta función devuelve un valor distinto después de evaluar múltiples opciones:
    (define (to-string arg)
(cond
((number? arg) (number->string arg))
((string? arg) arg)
((symbol? arg) " ")
((pair? arg) (my-pair->string arg))
(else " ")
)
)


(cond
((= inEdges 0) ; Laplacian edges
(plug-in-gauss-iir TRUE inImage EdgeLayer inWeight TRUE TRUE)
(plug-in-laplace TRUE inImage EdgeLayer)
) ; en first condition
((= inEdges 1) ; Sobel Edges
(plug-in-sobel TRUE inImage EdgeLayer TRUE TRUE TRUE)
) ; end second condition
((= inEdges 2) ; Synthetic Edges
(set! tmplayer (car (gimp-layer-copy EdgeLayer TRUE)))
(gimp-image-add-layer inImage tmplayer -1)
(gimp-layer-set-mode tmplayer DIVIDE-MODE)
(plug-in-gauss-iir TRUE inImage tmplayer inWeight TRUE TRUE)
(set! EdgeLayer
(car (gimp-image-merge-down inImage tmplayer
EXPAND-AS-NECESSARY)))
(gimp-levels EdgeLayer HISTOGRAM-VALUE
(- 255 inWeight) ; low input
255 ; high input
1.0 ; gamma
0 255) ; output
) ; end third condition
) ; end cond

3.4. begin

Sirve para acotar las opciones de un if, por ejemplo
    (begin
(func1 arg1 arg2)
(func2 arg4 arg5)
….........
)

(if (= inCopy TRUE)

(begin ; then ….
(gimp-image-clean-all theImage)
(gimp-display-new theImage)
)
(begin ; else …..
(gimp-message-set-handler 0)
)
)

        ; If the end layer is set below the start layer create
; an error message and terminate
(if (< theLayer1 theLayer2)
(begin
(set! theLayerRef (car (gimp-message-get-handler))
)
(gimp-message-set-handler 0)
(gimp-message "Error: End layer number must be set higher than ….!")
(gimp-message-set-handler theLayerRef)
)

3.3. If

Su esquema general es:
    (if (condicion)
(statementIf)
(statementElse)
)

Algunos ejemplos son:
    (if (= image-type 0)
(gimp-layer-set-mode layer-ufraw VALUE)
)

Este ejemplo comprueba si hay selección:
    (PreSelec (car (gimp-drawable-mask-bounds inDrawable)))
(if (eqv? PreSelec TRUE)
(set! inOrientacion 3)
)

(if (equal? TRUE noselection)
(gimp-selection-none inImage)
(gimp-selection-load selchannel)
)

Si se quieren poner más de una sentencia en un if, hay, que llamar a dos funciones diferentes:
    (if (< a b)
(FuntionThen arg1 a b)
(FuntionElse arg2 b a foo)
)

o bien utilizar la sentencia begin.

3.2. While

La única construcción looping que existe en Script-Fu es while
[Nota: esto se da en el actual interprete de SCHEME, SIOD, usado en Script-Fu. Una vez que el interprete de SCHEME planea cambiar a Guile,probablemente, serán añadidas más construcciones looping ].
Su esquema general es:

(while (condition)
(statement1)
(statement2)
….......
)

Algunos ejemplos son:

(while (< foo bar)
(func1 arg foo)
(func2 bar baz)
)

(set! i 0)
(while (< i 10)
(func foo i bar)
(set! i (+ i 1))
)

Aquí un ejemplo que dibuja líneas horizontales, de 16 pixels, en una imagen:

(set! y 0)
(while (< y size)
(gimp-rect-select img 0 y size 16 REPLACE 0 0)
(gimp-edit-fill layer-one BG-IMAGE-FILL)
(set! y (+ y 32))
)

miércoles, 10 de febrero de 2010

3.1 Comparaciones

Scheme tiene tres tipos diferentes de igualdad:
eq? : Devuelve #t si los dos objetos son exactamente el mismo objeto, comprobando incluso dónde están guardados físicamente.
        (if (eq? fierytog FALSE) 135.00 90.00)

eqv? : Normalmente igual que eq?, pero trata algunos objetos (por ejemplo, caracteres y números) de forma especial para que los números que sean iguales sean eqv? incluso si no son eq?.
        (if (eqv? inSmooth TRUE)

equal? : Compara el contenido de las estructuras de datos tales como listas, vectores y cadenas para determinar si son iguales.
        (equal? TRUE noselection)   

También existen en Scheme los operadores de equivalencia dependientes del tipo:
        string=? : Compara dos cadenas     ; (no he encontrado ejemplos)
char=? : Compara dos caracteres ; (no he encontrado ejemplos)

Comprobaciones de tipo:
        (number? arg)
(string? arg)
(symbol? arg)

Comparaciones de números
        (= image-type 0)
(< a b)


Otras comparaciones:
        (pair? arg) ;Compara si es una lista
(null? l) Compara si es nulo. P.e. (if (not (null? l))

OJO: Cuando vayamos a manejar variables introducidas a traves de la ventana de entrada hay que tener en cuenta el tipo de dato que nos devuelve la ventana