13

I note that Scheme and Lisp (I guess) support circular lists, and I have used circular lists in C/C++ to 'simplify' the insertion and deletion of elements, but what are they good for?

Scheme ensures that they can be built and processed, but for what?

Is there a 'killer' data structure that needs to be circular or tail-circular?

Svante
  • 46,788
  • 11
  • 77
  • 118
philcolbourn
  • 3,670
  • 3
  • 26
  • 31
  • I have now learned that Scheme's environment model requires (too strong?) circular lists: a procedure assigned in an environment 'points' back to it's environment - a circular list. – philcolbourn Dec 30 '11 at 13:08

5 Answers5

6

Saying it supports 'circular lists' is a bit much. You can build all kinds of circular data structures in Lisp. Like in many programming languages. There is not much special about Lisp in this respect. Take your typical 'Algorithms and Datastructure' book and implement any circular data structure: graphs, rings, ... What some Lisps offer is that one can print and read circular data structures. The support for this is because in typical Lisp programming domains circular data structures are common: parsers, relational expressions, networks of words, plans, ...

It is quite common that data structures contain cycles. Real 'circular lists' are not that often used. For example think of a task scheduler which runs a task and after some time switches to the next. The list of tasks can be circular so that after the 'last' task the scheduler takes the 'first' task. In fact there is no 'last' and 'first' - it is just a circular list of tasks and the scheduler runs them without end. You could also have a list of windows in a window system and with some key command you would switch to the next window. The list of windows could be circular.

Lists are useful when you need a cheap next operation and the size of the data structure is unknown in advance. You can always add another node to the list or remove a node from a list. Usual implementations of lists make getting the next node and adding/removing an item cheap. Getting the next element from an array is also relatively simple (increase the index, at the last index go to the first index), but adding/removing elements usually needs more expensive shift operations.

Also since it is easy to build circular data structures, one just might do it during interactive programming. If you then print a circular data structure with the built-in routines it would be a good idea if the printer can handle it, since otherwise it may print a circular list forever...

Rainer Joswig
  • 127,693
  • 10
  • 201
  • 325
  • By 'support' I was referring to specific syntax to describe circular lists for reading and writing (as you say). eg. `(#0=(1 2) (x y . #0#))`; also scheme's `list?` predicate specifies that a circular list is not a list - or is this implementation specific? – philcolbourn Mar 07 '10 at 12:23
  • Ok, I can see that a task scheduler is a circular-list. A good example. In LISP or Scheme, how would you insert or remove a task? – philcolbourn Mar 07 '10 at 12:28
  • 1
    @philcolbourn The specific syntax you mention works in Lisp for all kinds of data structures with cycles. Not just circular lists. You can use destructive list operations to add an object to a circular list. That's a typical programming exercise in Lisp and/or programming courses... – Rainer Joswig Mar 07 '10 at 13:18
  • OK, so circular lists make accessing the next item easy (no base-case) but require non-functional `set-car!` and `set-cdr!` operations to alter the list. – philcolbourn Mar 08 '10 at 09:54
4

Have you ever played Monopoly?

Without playing games with counters and modulo and such, how would you represent the Monopoly board in a computer implementation of the game? A circular list is a natural.

John R. Strohm
  • 7,254
  • 2
  • 24
  • 32
  • 2
    `how would you represent the Monopoly board in a computer implementation of the game?` ... As an array, so that I wouldn't have to iterate through each square on the board every move. – Cam Mar 07 '10 at 16:19
  • To be honest you would just keep a pointer to current square, so you would not have to that. – UK-AL May 04 '10 at 09:53
  • 1
    With a regular list, you would have to write logic to tell the position in the array to move from the end to the beginning. With a circular list, you'd just use the same logic no matter what: move X positions from current. Circular list would indeed be a natural here. – Dave Sep 11 '15 at 14:28
2

For example a double linked list data structure is "circular" in the Scheme/LISP point of view, i.e. if you try to print the cons-structure out you get backreferences, i.e. "cycles". So it's not really about having data structures that look like "rings", any data structure where you have some kind of backpointers is "circular" from the Scheme/LISP perspective.

A "normal" LISP list is single linked, which means that a destructive mutation to remove an item from inside the list is an O(n) operation; for double linked lists it is O(1). That's the "killer feature" of double linked lists, which are "circular" in the Scheme/LISP context.

Antti Huima
  • 23,825
  • 2
  • 50
  • 67
  • You say that 'normal' LISP lists are single linked - I understand that. But are you also saying that circular-lists in LISP/Scheme are doubly-linked? – philcolbourn Mar 07 '10 at 07:40
  • I think what he was saying was "if you have a double-linked list, it is circular". In lisp/scheme parlance, a circular list only means that there is at least one reference further down the list to at least one cell closer to the beginning. – Vatine Mar 07 '10 at 11:13
  • Yeah, he's saying that doubly linked lists fulfill the requirements for being 'circular'. – Cam Mar 07 '10 at 16:19
  • In C, a doubly-linked-list need not be circular although that might be a convention. I also don't think that deletion is O(1) as you would need to step through the list to find the item to delete. This would be O(n) wouldn't it? – philcolbourn Mar 08 '10 at 09:50
  • Deleting "the current element" in a double-linked list is O(1), deleting another element is O(N). Deleting "the current element" in a single-linked list is O(N), but deleting "the lemenet after the current element" is O(1). – Vatine Mar 09 '10 at 14:14
  • 1
    @philcolburn: A double-linked list in C fulfills the minimum criterion for a double-linked list, there is a reference to a "previous" as well as a "next" element. If there's at least one element where you can follow a sequence of pointers and end up at the original element, the structure is circular. – Vatine Mar 09 '10 at 14:16
2

Adding and removing elements to the beginning of a list is cheap. To add or remove an element from the end of a list, you have to traverse the whole list.

With a circular list, you can have a sort of fixed-length queue.

Setup a circular list of length 5:

> (import (srfi :1 lists))
> (define q (circular-list 1 2 3 4 5))

Let's add a number to the list:

 > (set-car! q 6)

Now, let's make that the last element of the list:

 > (set! q (cdr q))

Display the list:

 > (take q 5)
 (2 3 4 5 6)

So you can view this as a queue where elements enter at the end of the list and are removed from the head.

Let's add 7 to the list:

> (set-car! q 7)
> (set!     q (cdr q))
> (take q 5)
(3 4 5 6 7)

Etc...

Anyways, this is one way that I've used circular-lists.

I use this technique in an OpenGL demo which I ported from an example in the Processing book.

Ed

dharmatech
  • 7,399
  • 7
  • 32
  • 65
0

One use of circular lists is to "repeat" values when using the srfi-1 version of map. For example, to add val to each element of lst, we could write:

(map + (circular-list val) lst)

For example:

(map + (circular-list 10) (list 0 1 2 3 4 5))

returns:

(10 11 12 13 14 15)

Of course, you could do this by replacing + with (lambda (x) (+ x val)), but sometimes the above idiom can be handier. Note that this only works with the srfi-1 version of map, which can accept lists of different sizes.

SuperElectric
  • 13,992
  • 8
  • 43
  • 63