learning-sicp

My embarrassing half assed SICP run.
Log | Files | Refs

rear-delete-deque.scm (1039B)


      1 (define-library (sicp solutions 3_23 rear-delete-deque)
      2   (import (scheme base))
      3 
      4   (import (sicp solutions 3_23 empty-deque))
      5   (import (sicp solutions 3_23 front-ptr))
      6   (import (sicp solutions 3_23 rear-ptr))
      7   (import (sicp solutions 3_23 set-front-ptr))
      8   (import (sicp solutions 3_23 set-rear-ptr))
      9   (import (prefix (sicp solutions 3_23 doubly-linked-list) dll:))
     10 
     11   (export rear-delete-deque!)
     12 
     13   (begin
     14     (define (rear-delete-deque! deque)
     15       "Delete rearmost item of DEQUE if DEQUE is not empty, otherwise raise an error."
     16       (cond
     17        ((empty-deque? deque)
     18         (error "REAR-DELETE-DEQUE! called with an empty deque" deque))
     19        ((eq? (dll:link-rear (front-ptr deque))
     20              '()) ; The front link's rear pointer would point at '() only if it is the only link in the DEQUE.
     21         (set-front-ptr! deque '())
     22         (set-rear-ptr! deque '()))
     23        (else
     24         (set-rear-ptr! deque
     25                        (dll:link-front (rear-ptr deque)))
     26         (dll:delete-rear! (rear-ptr deque))))
     27       deque)))