正在加载图片...
6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology Slide 11.3.3 Now, if i decide to delete an element from this stack, my Alternative Stack Implementation-pg. 1 operation should get to the actual stack (everything but the tag),I.Attach a type tag-defensive programming mutate that structure to drop the first element, and reattach that. Additional benefit stack to the tag. Notice that by doing this, s still points to the Provides an object whose identity remains even as the same structure, that is to the cell with the symbolic tag and a (delete! pointer to the stack contents. This fixes the problem I saw previously, by maintaining a notion of the identity of the stack 4 6001 sICP Alternative Stack Implementation- pg 1 Slide 11.3. 4 Attach a type tag- defensive programming Notice that this is really a change in the contract. We should really advise the user that the stack is mutating, as this is provides an object whose identity remains even as the exactly what we have proposed here. We are proposing to (delete! s) mutate the stack to preserve the pointer from the stack's name to the tagged list, while mutating the part of the list that represents the stack's contents Note: This is a change to the abstraction! User should know if the object mutates or not in order to use th 601 SICP Slide 11.3.5 Alternative Stack Implementation- pg 2 o let's change our implementation to capture this idea First, our constructor must now create a tagged representation, where the actual contents are still an empty list. Note that this is (stack? stack) (pair? stack) (eq? ' stack (car stack)))) gluing together two different things, a symbolic tag, and an actual stack 《f(not《 stack?8t Given that, we can now create a predicate for stacks. Notice the (error object not a stack: stack) ull? (cdr stack)))) defensive programming in which we first ensure that the argument is a pair or list, before checking to see if the first element is the type tag We can also build our predicate for the empty stack. Notice that 6m: SICP it first checks to see that we have a tagged data structure of the appropriate kind. If we do, then we don' t have to further check to see if the object is a list that happened as part of the type check. So we can immediately proceed to the cdr of the argument. and check its contents6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. Slide 11.3.3 Now, if I decide to delete an element from this stack, my operation should get to the actual stack (everything but the tag), mutate that structure to drop the first element, and reattach that stack to the tag. Notice that by doing this, s still points to the same structure, that is to the cell with the symbolic tag and a pointer to the stack contents. This fixes the problem I saw previously, by maintaining a notion of the identity of the stack. Slide 11.3.4 Notice that this is really a change in the contract. We should really advise the user that the stack is mutating, as this is exactly what we have proposed here. We are proposing to mutate the stack to preserve the pointer from the stack's name to the tagged list, while mutating the part of the list that represents the stack's contents. Slide 11.3.5 So let's change our implementation to capture this idea. First, our constructor must now create a tagged representation, where the actual contents are still an empty list. Note that this is gluing together two different things, a symbolic tag, and an actual stack. Given that, we can now create a predicate for stacks. Notice the defensive programming in which we first ensure that the argument is a pair or list, before checking to see if the first element is the type tag. We can also build our predicate for the empty stack. Notice that it first checks to see that we have a tagged data structure of the appropriate kind. If we do, then we don't have to further check to see if the object is a list that happened as part of the type check. So we can immediately proceed to the cdr of the argument, and check its contents
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有