Home > other >  where does racket lambda argument come from?
where does racket lambda argument come from?

Time:10-03

I asked a similar question before, I just want to make sure I understand the idea, in the -lambda(x)- line 4, what is the x, where it is coming from?

(define (cached-assoc xs n)
  (letrec ([memo (make-vector n #f)]
           [acc 0]
           [f (lambda(x)
                (let ([ans (vector-assoc x memo)])
                  (if ans
                      (cdr ans)
                      (let ([new-ans (assoc x xs)])
                       (begin
                         (vector-set! memo acc (cons x new-ans))
                         (set! acc (if (= (  acc 1)) 0 (  acc 1)))
                         new-ans)))))])
  f))

CodePudding user response:

Your cached-assoc procedure returns f, a function which has x as an unbound parameter. It doesn’t have a value yet, the value will be whatever you pass to it when you finally invoke it:

((cached-assoc xs n) 10)

In the above example x will be bound to 10, and xs and n will be whatever you defined for them before calling cached-assoc. I believe the source of the confusion is the fact that cached-assoc is returning a lambda, once you understand this it'll be clear.

  • Related