#lang racket (define-namespace-anchor a) (define ns (namespace-anchor->namespace a)) ;; clasic (define prod1 (λ (argX argY) ; în argY primesc o valoare (displayln "prod") (if argX (* argY (+ argY 1)) 0))) (define test1 (λ (x) (let ((y 5)) (prod1 x (and (display "y ") y)))) ; în al doilea argument, prod primește valoarea 5 ) (test1 #f) (test1 #t) (displayln "------ ^ direct") ; îmi doresc să obțin ; prod ; prod y (define prod2 (λ (argX argY) ; în argY primesc o secvență de literali (displayln "prod") (if argX (* (eval argY ns) (+ (eval argY ns) 1)) 0))) ; nu văd simbolul y ca identificator în acest context (define test2 (λ (x) (let ((y 5)) (prod2 x (quote (and (display "y ") y))))) ) ;(test2 #f) (test2 #t) (displayln "------ ^ eval") (define calcul '(+ 1 3)) (define prod3 (λ (argX argY) ; în argY primesc o închidere (displayln "prod") (if argX (* (argY) (+ (argY) 1)) 0))) (define test3 (λ (x) (let ((y 5)) (prod3 x (λ () (and (display "y ") y))))) ; în al doilea argument, prod primește o închidere funcțională ; < (λ () (and (display "y ") y)) ; { yarg <- 5 } > ) (test3 #f) (test3 #t) (displayln "------ ^ închideri") (define prod4 (λ (argX argY) ; în argY primesc o promisiune (displayln "prod") (if argX (* (force argY) (+ (force argY) 1)) 0))) ; în al doilea argument, prod primește o promisiune ; < (and (display "y ") yarg) ; { yarg <- 5 } ; fără valoare calculată > ; după primul force, promisiunea devine ; < (and (display "y ") yarg) ; { yarg <- 5 } ; valoare calculată: 5> ; la force-uri ulterioare pe această promisiune, se întoarce direct valoarea calculată (define test4 (λ (x) (let ((y 5)) (prod4 x (delay (and (display "y ") y))))) ) (test4 #f) (test4 #t) (displayln "------ ^ promisiuni")