[racket] doubly linked list lib

From: Marijn (hkBst at gentoo.org)
Date: Wed Aug 31 05:46:34 EDT 2011

Hi Laurent,

On 08/30/11 09:18, Laurent wrote:
> Thank you very much for this nice intermediate solution, though I need
> constant-time append, split, insert, remove, + pointers to items, etc.
> Mutation does seem unavoidable, right.

I implemented a doubly-linked list, not so long ago, connected to a GUI
that can insert and delete items and saw no way to make the list
functional with multiple simultaneous editors in the GUI. The
implementation is as a straightforward cyclical doubly-linked list. I
toyed with the idea of having a separate handle object to represent the
list versus just the nodes, and there are some rudiments of that left in
the code, but in the end the user code uses a special 'top element to
indicate where the cyclical list is supposed to start.

Good luck,

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: dlist.rkt
URL: <http://lists.racket-lang.org/users/archive/attachments/20110831/7ae8c25e/attachment.ksh>
-------------- next part --------------
#lang racket/gui

;(require dlist)
(require "./dlist.rkt")

(define list-editor%
  (class vertical-panel%
    (init init-values parent)
    (super-new (parent parent))
    (define widget-list (dlist 'top))
    (define (redisplay)
      (send this change-children (lambda (l) (cdr (for/list ((w widget-list)) w)))))
    (define (insert-item val link)
      (let* ((v (new vertical-panel% (parent this)))
             (lk (dl-insert v link))
             (ins (new button% (parent v) (label "insert")
                       (callback (? (b e)
                                   (insert-item "1" lk) (redisplay) )) ) )
             (h (new horizontal-pane% (parent v)))
             (t (new text-field% (parent h) (label "") (init-value val)))
             (del (new button% (parent h) (label "del")
                       (callback (? (b e) (dl-remove lk) (send this delete-child v))) )))
;    (send this begin-container-sequence)
    (for ((v init-values)) (insert-item v widget-list))
;    (send this end-container-sequence)
    (let* ((v (new vertical-panel% (parent this)))
           (lk (dl-insert v widget-list)))
      (new button% (parent v) (label "append")
           (callback (? (b e) (insert-item "1" lk) (redisplay))) ))
    )) ; end define class

(define root (new frame% (label "List Editor") (stretchable-height #f)))
(new list-editor% (parent root) (init-values '("1" "2" "3")))

(send root show #t)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 262 bytes
Desc: OpenPGP digital signature
URL: <http://lists.racket-lang.org/users/archive/attachments/20110831/7ae8c25e/attachment.sig>

Posted on the users mailing list.