130 likes | 256 Vues
In Scheme, while procedure calls are typically managed using a stack, heaps provide a powerful alternative for saving procedure states. This allows for more flexible memory management, particularly in recursive function calls and complex data structures. This guide explores the mechanics of heaps in Scheme, detailing how they can be utilized to store procedure states effectively and enhance the control flow of programs. Understanding heaps opens up new possibilities for optimizing performance and functionality in Scheme programming.
E N D
Heaps in Scheme Normally, procedure calls are saved on a stack
Heaps in Scheme Normally, procedure calls are saved on a stack f (n) { int x = 1; g(x); f:x=1
Heaps in Scheme Normally, procedure calls are saved on a stack g(n) { int x = 2; h(x); f (n) { int x = 1; g(x); g:x=2 f:x=1
Heaps in Scheme Normally, procedure calls are saved on a stack g(n) { int x = 2; h(x); h(n) { int x = 3; } f (n) { int x = 1; g(x); g:x=2 f:x=1
Heaps in Scheme Normally, procedure calls are saved on a stack g(n) { int x = 2; h(x); } h(n) { int x = 3; } f (n) { int x = 1; g(x); f:x=1
Heaps in Scheme Normally, procedure calls are saved on a stack g(n) { int x = 2; h(x); } h(n) { int x = 3; } f (n) { int x = 1; g(x); }
Heaps in Scheme But in Scheme a heap is used to save proc state
Heaps in Scheme But in Scheme a heap is used to save proc state (f 1 (g f:1
Heaps in Scheme But in Scheme a heap is used to save proc state (f 1 (g 2 (h (set! c (call/cc (lambda (k) k)) c g:2 f:1
Heaps in Scheme But in Scheme a heap is used to save proc state (f 1 (g 2 (h (set! c (call/cc (lambda (k) k))) c g:2 f:1
Heaps in Scheme But in Scheme a heap is used to save proc state (f 1 (g 2 (h (set! c (call/cc (lambda (k) k))))) (g 3 (h ... c g:3 g:2 f:1
Heaps in Scheme But in Scheme a heap is used to save proc state (f 1 (g 2 (h (set! c (call/cc (lambda (k) k))))) (g 3 (h ... c To return to a previous point in time: (c 4) Has the effect of returning from the set! with value 4 g:3 g:2 f:1