help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: 'length' function for lists and cons cells?


From: Pascal J. Bourguignon
Subject: Re: 'length' function for lists and cons cells?
Date: Mon, 25 Mar 2013 21:02:59 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux)

duthen.cnv@gmail.com writes:

> Considering that:
>  (a b)     contains 2 cons cells and 2 values (a and b),
>  (a b . c) contains 2 cons cells and 3 values (a b and c),
>  (a b c)   contains 3 cons cells and 3 values (a b and c).
>
> So, one possible point of vue (not necessarily mine, though!) 
> could consider that (a b . c) is "a little bit longer" than (a b) 
> and "a little bit shorter" than (a b c)!
>
> Hence, the function:
> (defun semi-length (xs)
>   (cond ((null xs)  0)
>         ((atom xs)  .5)
>         (t (1+ (semi-length (cdr xs))))))
>
> (mapcar (lambda (x) (cons (semi-length x) x))
>        '(()
>          a
>          (a)
>          (a . b)
>          (a b)
>          (a b . c)
>          (a b c)))
>
> ((0) (0.5 . a) (1 a) (1.5 a . b) (2 a b) (2.5 a b . c) (3 a b c))
>
> A true list is just one whith an integer semi-length
> whereas a dotted-paired list is one with a fractional semi-length ! :)

Nice.  And since circular lists have a stem length and a loop length,
with the loop length >0, we can represent them as complex numbers,
perhaps something like:

    (* (+ stem-length loop-length) 
       (expt (sqrt -1) (/ stem-length loop-length))) 


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]