How can I increment a value in scheme with closure?

runners3431

How can I increment a value in scheme with closure? I'm on lecture 3A in the sicp course.

(define (sum VAL)

    // how do I increment VAL everytime i call it?

    (lambda(x)  
        (* x x VAL)))

(define a (sum 5))

(a 3)
Óscar López

Use set! for storing the incremented value. Try this:

(define (sum VAL)
  (lambda (x)
    (set! VAL (add1 VAL))
    (* x x VAL)))

Because VAL was enclosed at the time the sum procedure was called, each time you call a it'll "remember" the previous value in VAL and it'll get incremented by one unit. For example:

(define a (sum 5)) ; VAL = 5

(a 3)  ; VAL = 6
=> 54  ; (* 3 3 6)

(a 3)  ; VAL = 7
=> 63  ; (* 3 3 7)

Answering the comment: sure, you can use let, but it's not really necessary, it has the same effect as before. The difference is that in the previous code we modified an enclosed function parameter and now we're modifying an enclosed let-defined variable, but the result is identical. However, this would be useful if you needed to perform some operation on n before initializing VAL:

(define (sum n)
  (let ((VAL n))
    (lambda (x)
      (set! VAL (add1 VAL))
      (* x x VAL))))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I modify a value outside of a closure from the closure?

How can I increment a value on page refresh?

How can I increment a value from firebase?

How can I access the functions in the return value of this closure

How can I do own increment value in mysql

How can I increment a variable without exceeding a maximum value?

How can I increment value if condition matches by Redux?

How can I increment value of json data if it meets certain conditions?

How can I auto-increment the value in a dictionary when there is a difference?

How can I increment an Angular ng-repeat function value?

Can I get the updated value of a variable in a closure?

How can I increment a char?

How can I reset an increment?

How can I increment the Array

How can I move a captured variable into a closure within a closure?

How can I store a closure that returns another closure?

Can I atomically increment a column value in BigTable?

How can I redefine '+' inside of a closure?

How can I access an array outside of a closure?

How can I use DispatchSemaphore with a Closure

Python: How can I assert the origin of a closure?

How can I make a closure to conform to 'AccessibilityRotorContent'?

How can I store a closure object in a struct?

How can I obtain the address of a closure in Rust?

MySQL: How can I use InnoDB locking to increment a non-primary value while preventing duplication of that value?

how can i get value of multiple checked box and perform increment using value from checked box

How can I convert a file Uri scheme into content Uri scheme?

How can i send a value to another file and then increment the same amount of times as i make the code iterate?

how can i just increment a variable so that it store the value i want it to?