[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Scheme-reports] Query - Pairs and Lists

On 03/04/2012 12:41 PM, Eli Barzilay wrote:
> 30 minutes ago, Per Bothner wrote:
>> You could have a bit or separate typecode for non-list cons
>> cells - for example LIST_PAIR vs NON_LIST_PAIR.  Cons creates
>> a LIST_PAIR if the cdr is '() or a LIST_PAIR, and creates a
>> NON_LIST_PAIR otherwise.  set-cdr! checks the new cdr if it
>> is a NON_LIST_PAIR *or* if the set-cdr! would create a cycle.
>> In that case checking for a pure list is O(1).  Only set-cdr!
>> becomes order-of-magnitude slower, but it's not a function
>> we want to encourage use of anyway.
> `set-cdr!' is much worse:
>    (define foo (cons 1 '()))
>    (define bar (cons 1 foo))
>    (list? bar)
>    (set-cdr! foo 1)
>    (list? bar)

Indeed.  In which case the idea of two kinds of cons operator
(creating a LIST_PAIR and NON_LIST_PAIR, respectively),
is appealing - at least if we were starting from scratch.
	--Per Bothner
per@x   http://per.bothner.com/

Scheme-reports mailing list