mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-17 20:11:57 +02:00
[racket/es] Parameters (#4071)
This commit is contained in:
@@ -672,12 +672,62 @@ vec ; => #(1 2 3 4)
|
|||||||
(call-with-input-file "/tmp/tmp.txt"
|
(call-with-input-file "/tmp/tmp.txt"
|
||||||
(λ (in-port)
|
(λ (in-port)
|
||||||
(displayln (read-line in-port))))
|
(displayln (read-line in-port))))
|
||||||
|
|
||||||
|
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
;; 12. Parametros
|
||||||
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
|
;; Racket tiene el concepto de "parameter"
|
||||||
|
;; El cual es un tipo de funcion similar un variable global entre modulos
|
||||||
|
|
||||||
|
;; Defino x y lo cambio localmente
|
||||||
|
(define x 42)
|
||||||
|
(set! x 43)
|
||||||
|
x ; 43
|
||||||
|
|
||||||
|
;; Ahora x es parte un modulo
|
||||||
|
(module mod racket
|
||||||
|
(provide x-mod)
|
||||||
|
(define x-mod 42))
|
||||||
|
;; incluyo x e intento cambiarlo
|
||||||
|
(require 'mod)
|
||||||
|
x-mod ; 42
|
||||||
|
(set! x-mod 43) ; error: cannot mutate x
|
||||||
|
;; Aca es donde tiene utilidad el uso de parametros
|
||||||
|
(module mod-param racket
|
||||||
|
(provide my-param)
|
||||||
|
(define my-param (make-parameter 42))) ;; creo un parametro con (make-parameter <valor-por-defecto>)
|
||||||
|
|
||||||
|
(require 'mod-param)
|
||||||
|
(my-param) ; 42
|
||||||
|
(my-param 43) ;; ahora el valor x es cambiado para todo el ambiente del modulo donde se esta ejecutando
|
||||||
|
(my-param) ; 43
|
||||||
|
;; Tambien puedo asignar un valor a un parametro en un ambiente local simil let
|
||||||
|
;; devuelve el ultimo valor del BODY (parameterize ([ID EXPR] BODY ... )
|
||||||
|
(parameterize
|
||||||
|
([ my-param "un valor de tipo distinto"])
|
||||||
|
(displayln (my-param)))
|
||||||
|
"un valor de tipo distinto" ;; x cambio solo el ambiente local de parameterize
|
||||||
|
(my-param) ;; 43
|
||||||
|
;; mi modulo tiene un funcion con parametros que cambia su comportamiento según el parametro
|
||||||
|
(module my-mod racket
|
||||||
|
(provide my-add verbose)
|
||||||
|
(define verbose (make-parameter #f)) ;; Parametro
|
||||||
|
(define (my-add a b ) ;; funcion
|
||||||
|
(define result (+ a b))
|
||||||
|
(when (verbose) (display (format "verbose: (my-add ~a ~a) => ~a~n" a b result)))
|
||||||
|
result)) ;; creo un parametro con (make-parameter <valor-por-defecto>)
|
||||||
|
(require 'my-mod)
|
||||||
|
(my-add 3 4)
|
||||||
|
;; 7
|
||||||
|
(verbose #f)
|
||||||
|
(my-add 3 4)
|
||||||
|
;; 7
|
||||||
|
(+ 1 (parameterize ([ verbose #t]) (my-add 3 4 )))
|
||||||
|
;; 8
|
||||||
```
|
```
|
||||||
|
|
||||||
## Mas información
|
## Mas información
|
||||||
|
|
||||||
¿Quieres saber mas? Prueba en [Empezando con Racket](http://docs.racket-lang.org/getting-started/)
|
¿Quieres saber mas? Prueba en [Empezando con Racket](http://docs.racket-lang.org/getting-started/)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user