Anonymous lambdas directly refer to themselves

Does the circuit or any dialects of the circuit have some kind of "I" operator, so that anonymous lambdas can be repeated on their own without doing something like Y-combinator or are called in letrec, etc.

Sort of:

(lambda (n)
   (cond
     ((= n 0) 1)
     (else (* n (self (- n 1)))))))
+5
source share
3 answers

No. The problem with the “current lambda” is that there are a lot of hidden lambdas on the Scheme. For instance:

  • All forms let(including let*, letrecand named let)
  • do(which expands to a name let)
  • delay, lazy, receiveEtc.

, , , , .

, .

+9

"" , . syntax-case, letrec lambda. , ( , alambda self).

;; Define a version of lambda that binds the
;; anaphoric variable "self" to the function
;; being defined.
;;
;; Note the use of datum->syntax to specify the
;; scope of the anaphoric identifier. 
(define-syntax alambda
  (lambda (stx)
    (syntax-case stx ()
      [(alambda lambda-list . body)
       (with-syntax ([name (datum->syntax #'alambda 'self)])
         #'(letrec ([name (lambda lambda-list . body)])
             name))])))

;; We can define let in terms of alambda as usual.
(define-syntax let/alambda
  (syntax-rules ()
    [(_ ((var val) ...) . body)
     ((alambda (var ...) . body) val ...)]))

;; The let/alambda macro does not shadow the outer
;; alambda anaphoric variable, which is lexical
;; with regard to the alambda form.
((alambda (n)
   (if (zero? n)
       1
       (let/alambda ([n-1 (- n 1)])
         (* (self n-1) n))))
 10)
;=> 3628800

, . , . (, , let/alambda alambda. self, , , ). . "" lambda, syntax-rules:

;; Define a version of lambda that allows the
;; user to specifiy a name for the function
;; being defined.
(define-syntax llambda
  (syntax-rules ()
    [(_ name lambda-list . body)
     (letrec ([name (lambda lambda-list . body)])
       name)]))

;; The factorial function can be expressed
;; using llambda.
((llambda fac (n)
   (if (zero? n)
       1
       (* (fac (- n 1)) n)))
 10)
;=> 3628800
+6

, lambdas , Racket, , "". , , racket, , , .

, .

( (lambda (n)
     (call-with-values 
       (lambda () (call-with-composable-continuation  
                        (lambda (k) (values k n))))
     (lambda (k n)
        (cond 
          [(= 0 n) 1]
          [else (* n (k k (- n 1)))])))) 5)  

k - , , . , (k k N), , ( , ).

. (self ARGS...) (k k ARGS...)

:

 ((lambda-with-self (n)
    (cond 
      [(= 0 n) 0]
      [(= 1 n) 1]
      [else (* n (self (- n 1)))])) 5)

Racket :

#lang racket
(require racket/stxparam) ;required for syntax-parameters
(  define-syntax-parameter self (λ (stx) (raise-syntax-error #f "not in  `lambda-with-self'" stx)))

(define-syntax-rule
(lambda-with-self (ARG ... ) BODY ...) 
 (lambda (ARG ...)
   (call-with-values 
    (lambda ()(call/comp (lambda (k) (values k ARG ...))))
    (lambda (k ARG ...)
      (syntax-parameterize ([self (syntax-rules ( )[(self ARG ...) (k k ARG ...)])])
    BODY ...)))))
;Example using factorial function
((lambda-with-self (n)
      (cond 
        [(= 0 n) 0]
        [(= 1 n) 1]
        [else (* n (self (- n 1)))])) 5)

.    Racket

, call-with-current-continue, call-with-composable-continue , , .

0

All Articles