unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Element-Relative Sequence Insertion
@ 2009-06-11 12:11 Nordlöw
  2009-06-11 12:38 ` Pascal J. Bourguignon
  0 siblings, 1 reply; 8+ messages in thread
From: Nordlöw @ 2009-06-11 12:11 UTC (permalink / raw)
  To: help-gnu-emacs

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)

Thanks in advance,
Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 12:11 Element-Relative Sequence Insertion Nordlöw
@ 2009-06-11 12:38 ` Pascal J. Bourguignon
  2009-06-11 12:51   ` Nordlöw
  0 siblings, 1 reply; 8+ messages in thread
From: Pascal J. Bourguignon @ 2009-06-11 12:38 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> 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.


\f

(qrsha vafreg-nsgre (arj yvfg byq)
   (pbaq ((ahyy yvfg) '())
         ((rdy byq (pne yvfg))  (pbaf (pne yvfg) (pbaf arj (pqe yvfg))))
         (g (pbaf (pne yvfg) (vafreg-nsgre arj (pqe yvfg) byq)))))

(qrsha vafreg-orsber (arj yvfg byq)
   (pbaq ((ahyy yvfg) '())
         ((rdy byq (pne yvfg))  (pbaf arj yvfg))
         (g (pbaf (pne yvfg) (vafreg-orsber arj (pqe yvfg) byq)))))

-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 12:38 ` Pascal J. Bourguignon
@ 2009-06-11 12:51   ` Nordlöw
  2009-06-11 13:01     ` Pascal J. Bourguignon
  0 siblings, 1 reply; 8+ messages in thread
From: Nordlöw @ 2009-06-11 12:51 UTC (permalink / raw)
  To: help-gnu-emacs

On 11 Juni, 14:38, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Nordlöw <per.nord...@gmail.com> 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.
>
> (qrsha vafreg-nsgre (arj yvfg byq)
>    (pbaq ((ahyy yvfg) '())
>          ((rdy byq (pne yvfg))  (pbaf (pne yvfg) (pbaf arj (pqe yvfg))))
>          (g (pbaf (pne yvfg) (vafreg-nsgre arj (pqe yvfg) byq)))))
>
> (qrsha vafreg-orsber (arj yvfg byq)
>    (pbaq ((ahyy yvfg) '())
>          ((rdy byq (pne yvfg))  (pbaf arj yvfg))
>          (g (pbaf (pne yvfg) (vafreg-orsber arj (pqe yvfg) byq)))))
>
> --
> __Pascal Bourguignon__

But how do I implement insert-after() and insert-before()? I guess the
code in the end doesn't do anything meaningful :)

/Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 12:51   ` Nordlöw
@ 2009-06-11 13:01     ` Pascal J. Bourguignon
  2009-06-11 14:18       ` Marc Tfardy
  0 siblings, 1 reply; 8+ messages in thread
From: Pascal J. Bourguignon @ 2009-06-11 13:01 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> On 11 Juni, 14:38, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> Nordlöw <per.nord...@gmail.com> 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.
>>
>> (qrsha vafreg-nsgre (arj yvfg byq)
>>    (pbaq ((ahyy yvfg) '())
>>          ((rdy byq (pne yvfg))  (pbaf (pne yvfg) (pbaf arj (pqe yvfg))))
>>          (g (pbaf (pne yvfg) (vafreg-nsgre arj (pqe yvfg) byq)))))
>>
>> (qrsha vafreg-orsber (arj yvfg byq)
>>    (pbaq ((ahyy yvfg) '())
>>          ((rdy byq (pne yvfg))  (pbaf arj yvfg))
>>          (g (pbaf (pne yvfg) (vafreg-orsber arj (pqe yvfg) byq)))))
>>
>> --
>> __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.

Could you implement first something simplier than insert-after or
insert-before?


For example, could you implement a function map, which builds a new
list with the result of calling a function to each element of a list:

   (defun map (function list) ...) 

   (map (lambda (x) (1+ x)) '(1 2 3)) --> (2 3 4)
   (map (lambda (x) (1+ x)) '())      --> ()

If  you can do that, then you should be able to implement
insert-before and insert-after adding only one line.


-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 13:01     ` Pascal J. Bourguignon
@ 2009-06-11 14:18       ` Marc Tfardy
  2009-06-11 14:36         ` Pascal J. Bourguignon
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Tfardy @ 2009-06-11 14:18 UTC (permalink / raw)
  To: help-gnu-emacs

Pascal J. Bourguignon schrieb:
> Nordlöw <per.nordlow@gmail.com> writes:
> 
>> On 11 Juni, 14:38, p...@informatimago.com (Pascal J. Bourguignon)
>> wrote:
>>> Nordlöw <per.nord...@gmail.com> 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.
>>>
>>> (qrsha vafreg-nsgre (arj yvfg byq)
>>>    (pbaq ((ahyy yvfg) '())
>>>          ((rdy byq (pne yvfg))  (pbaf (pne yvfg) (pbaf arj (pqe yvfg))))
>>>          (g (pbaf (pne yvfg) (vafreg-nsgre arj (pqe yvfg) byq)))))
>>>
>>> (qrsha vafreg-orsber (arj yvfg byq)
>>>    (pbaq ((ahyy yvfg) '())
>>>          ((rdy byq (pne yvfg))  (pbaf arj yvfg))
>>>          (g (pbaf (pne yvfg) (vafreg-orsber arj (pqe yvfg) byq)))))
>>>
>>> --
>>> __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? ;-)

regards
Marc



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 14:18       ` Marc Tfardy
@ 2009-06-11 14:36         ` Pascal J. Bourguignon
  2009-06-11 15:01           ` Marc Tfardy
  2009-06-13 16:47           ` Johan Bockgård
  0 siblings, 2 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2009-06-11 14:36 UTC (permalink / raw)
  To: help-gnu-emacs

Marc Tfardy <bum@cyk.cyk> writes:

> Pascal J. Bourguignon schrieb:
>> Nordlöw <per.nordlow@gmail.com> writes:
>>
>>> On 11 Juni, 14:38, p...@informatimago.com (Pascal J. Bourguignon)
>>> wrote:
>>>> Nordlöw <per.nord...@gmail.com> 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__


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 14:36         ` Pascal J. Bourguignon
@ 2009-06-11 15:01           ` Marc Tfardy
  2009-06-13 16:47           ` Johan Bockgård
  1 sibling, 0 replies; 8+ messages in thread
From: Marc Tfardy @ 2009-06-11 15:01 UTC (permalink / raw)
  To: help-gnu-emacs

 >>> 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!

OK, this was a mistake. But it's "only" cosmetics ;-) I tested this
with:

(insert-after 'X '(a b c) 'b)
(a b X c)

(insert-before 'X '(a b c) 'b)
(a X b c)

so I unintended assumed your right arguments order ;)


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

Yes, this was my big fault. Sorry.


 > And bad points for not being able to use rot13.
 >
 > You lose.   :-)

Oh shit... :-)

regards
Marc




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Element-Relative Sequence Insertion
  2009-06-11 14:36         ` Pascal J. Bourguignon
  2009-06-11 15:01           ` Marc Tfardy
@ 2009-06-13 16:47           ` Johan Bockgård
  1 sibling, 0 replies; 8+ messages in thread
From: Johan Bockgård @ 2009-06-13 16:47 UTC (permalink / raw)
  To: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

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

The normal equality predicate for Emacs Lisp is `equal'.





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-06-13 16:47 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-11 12:11 Element-Relative Sequence Insertion Nordlöw
2009-06-11 12:38 ` Pascal J. Bourguignon
2009-06-11 12:51   ` Nordlöw
2009-06-11 13:01     ` Pascal J. Bourguignon
2009-06-11 14:18       ` Marc Tfardy
2009-06-11 14:36         ` Pascal J. Bourguignon
2009-06-11 15:01           ` Marc Tfardy
2009-06-13 16:47           ` Johan Bockgård

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).