[Top][All Lists]

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

Re: Element-Relative Sequence Insertion

From: Pascal J. Bourguignon
Subject: Re: Element-Relative Sequence Insertion
Date: Thu, 11 Jun 2009 16:36:25 +0200
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/22.2 (gnu/linux)

Marc Tfardy <address@hidden> writes:

> Pascal J. Bourguignon schrieb:
>> Nordlöw <address@hidden> writes:
>>> On 11 Juni, 14:38, address@hidden (Pascal J. Bourguignon)
>>> wrote:
>>>> Nordlöw <address@hidden> writes:
>>>>> How do I insert an element (object) relative to another in a sequence
>>>>> (or list in my case)? Example:
>>>>> (insert-after 'X '(a b c) 'b)  => '(a b X c)
>>>>> (insert-before 'X '(a b c) b)  => '(a X b c)
>>>> This is not what you want.  You don't what these function to return a
>>>> 2-element lists, the first of which is the symbol quote.  Unless
>>>> you're doing meta-programming you never want to have such a result.
>>>> You wan this:
>>>>    (insert-after 'X '(a b c)  'b)  -->  (a b X c)
>>>>    (insert-before 'X '(a b c) 'b)  -->  (a X b c)
>>>> Also, you may want to mention whether your insert-after and
>>>> insert-before function are destructive or non-destructive.  Since you
>>>> show as only specification examples using literal data, we will assume
>>>> you want them to be non-destructive.
>>>> So you do that, just by doing it.  There's nothing simplier.
>>>> (defun insert-after (new list old)
>>>>    (cond ((null list) '())
>>>>          ((eql old (car list))  (cons (car list) (cons new (cdr list))))
>>>>          (t (cons (car list) (insert-after new (cdr list) old)))))
>>>> (defun insert-before (new list old)
>>>>    (cond ((null list) '())
>>>>          ((eql old (car list))  (cons new list))
>>>>          (t (cons (car list) (insert-before new (cdr list) old)))))
>>>> --
>>>> __Pascal Bourguignon__
>>> But how do I implement insert-after() and insert-before()? I guess the
>>> code in the end doesn't do anything meaningful :)
>> It does, but you have to decrypt it first.
> (defun insert-after (elt lst new)
>   (cond ((null lst)
>          '())
>         ((eq new (car lst))
>          (cons (car lst) (cons elt (cdr lst))))
>         (t
>          (cons (car lst) (insert-after elt (cdr lst) new)))))
> (defun insert-before (elt lst new)
>   (cond ((null lst)
>          '())
>         ((eq new (car lst))
>          (cons elt lst))
>         (t
>          (cons (car lst) (insert-before elt (cdr lst) new)))))
> What is the price? ;-)

What's the point in renaming the parameters and introducing bugs?

Check again the order of the parameters!

Notice that (eq 3.141592 3.141592) --> nil ; which is the reason why
eql should be used by default.

And bad points for not being able to use rot13.

You lose.   :-)
__Pascal Bourguignon__

reply via email to

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