fix queue/stack confusion in the Guide
closes PR 12896
This commit is contained in:
parent
e430463ea4
commit
0ca54bfe56
|
@ -9,7 +9,7 @@ protect abstractions, ensuring that clients of your module
|
||||||
cannot depend on the precise representation choices you make
|
cannot depend on the precise representation choices you make
|
||||||
for your data structures.
|
for your data structures.
|
||||||
|
|
||||||
@; @ctc-section{Getting Started, with a Stack Example}
|
@; @ctc-section{Getting Started, with a Queue Example}
|
||||||
|
|
||||||
@margin-note{
|
@margin-note{
|
||||||
You can type @racket[#:exists] instead of @racket[#:∃] if you
|
You can type @racket[#:exists] instead of @racket[#:∃] if you
|
||||||
|
@ -21,7 +21,7 @@ The @racket[contract-out] form allows you to write
|
||||||
introduces the variable @racket[_name-of-a-new-contract], binding it to a new
|
introduces the variable @racket[_name-of-a-new-contract], binding it to a new
|
||||||
contract that hides information about the values it protects.
|
contract that hides information about the values it protects.
|
||||||
|
|
||||||
As an example, consider this (simple) implementation of a stack datastructure:
|
As an example, consider this (simple) implementation of a queue datastructure:
|
||||||
@racketmod[racket
|
@racketmod[racket
|
||||||
(define empty '())
|
(define empty '())
|
||||||
(define (enq top queue) (append queue (list top)))
|
(define (enq top queue) (append queue (list top)))
|
||||||
|
@ -42,16 +42,16 @@ data structure (perhaps accidentally) and thus any change in the representation
|
||||||
(say to a more efficient representation that supports amortized constant time
|
(say to a more efficient representation that supports amortized constant time
|
||||||
enqueue and dequeue operations) might break client code.
|
enqueue and dequeue operations) might break client code.
|
||||||
|
|
||||||
To ensure that the stack representation is abstract, we can use @racket[#:∃] in the
|
To ensure that the queue representation is abstract, we can use @racket[#:∃] in the
|
||||||
@racket[contract-out] expression, like this:
|
@racket[contract-out] expression, like this:
|
||||||
@racketblock[(provide
|
@racketblock[(provide
|
||||||
(contract-out
|
(contract-out
|
||||||
#:∃ stack
|
#:∃ queue
|
||||||
[empty stack]
|
[empty queue]
|
||||||
[enq (-> integer? stack stack)]
|
[enq (-> integer? queue queue)]
|
||||||
[next (-> stack integer?)]
|
[next (-> queue integer?)]
|
||||||
[deq (-> stack (listof integer?))]
|
[deq (-> queue (listof integer?))]
|
||||||
[empty? (-> stack boolean?)]))]
|
[empty? (-> queue boolean?)]))]
|
||||||
|
|
||||||
Now, if clients of the data structure try to use @racket[car] and @racket[cdr], they
|
Now, if clients of the data structure try to use @racket[car] and @racket[cdr], they
|
||||||
receive an error, rather than mucking about with the internals of the queues.
|
receive an error, rather than mucking about with the internals of the queues.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user