In article <·······················@r23g2000prd.googlegroups.com>,
·············@gmail.com wrote:
> Hi everyone,
>
> I'm new to Lisp. I'm still reading the book "Essential Lisp" by
> myself. I'm asked by this excercise to define a function insert that
> inserts a number to a list of number of ascending order. I'm only
> allowed to create only one cons cell.
>
> This is the way I coded:
>
> (defun insert (n l)
> (cond ((null l) nil)
> ((<= n (car l)) (cons n l))
> (t (rplacd l (insert n (cdr l))))))
>
> Because I'm still new to Lisp and this destructive thing, I'm unsure
> my function is destructive or not. As well,
> could you shed some lights on the benefits of destructive function in
> this particular function.
Others have answered your specific question. I'd like to point out that
your function doesn't work in the case where you're inserting the first
number into an empty list, or inserting a number that's greater than all
the elements of the list.
--
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
On Aug 19, 4:44 pm, Barry Margolin <······@alum.mit.edu> wrote:
> In article <·······················@r23g2000prd.googlegroups.com>,
>
>
>
> ·············@gmail.com wrote:
> > Hi everyone,
>
> > I'm new to Lisp. I'm still reading the book "Essential Lisp" by
> > myself. I'm asked by this excercise to define a function insert that
> > inserts a number to a list of number of ascending order. I'm only
> > allowed to create only one cons cell.
>
> > This is the way I coded:
>
> > (defun insert (n l)
> > (cond ((null l) nil)
> > ((<= n (car l)) (cons n l))
> > (t (rplacd l (insert n (cdr l))))))
>
> > Because I'm still new to Lisp and this destructive thing, I'm unsure
> > my function is destructive or not. As well,
> > could you shed some lights on the benefits of destructive function in
> > this particular function.
>
> Others have answered your specific question. I'd like to point out that
> your function doesn't work in the case where you're inserting the first
> number into an empty list, or inserting a number that's greater than all
> the elements of the list.
>
> --
> Barry Margolin, ······@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***
Thanks a lot for pointing out.
Mark.