I am trying to write the insertion sort routine this is what I got so far
(defun insertion_sort (x y)
(cond
((null x) (cons(x y)))
((atom x) (cons(x y)))
((= (car(x) y) ))
((> (car(x) y)) (cons(y x)))
((< (car(x) y)) (insertion_sort((cdr(x)) y)))
)
)
Is this right?
Slapstiq
Slapstiq <········@rochester.rr.com> wrote in message
························@typhoon.nyroc.rr.com...
> I am trying to write the insertion sort routine this is what I got so far
>
> (defun insertion_sort (x y)
> (cond
> ((null x) (cons(x y)))
> ((atom x) (cons(x y)))
> ((= (car(x) y) ))
> ((> (car(x) y)) (cons(y x)))
> ((< (car(x) y)) (insertion_sort((cdr(x)) y)))
> )
> )
>
> Is this right?
Did you try it? Did it work?
(You didn't, it won't.)
First, remember Lisp syntax: parentheses are not used
for grouping, they indicate a function call. So you would
not write (car (x)) [unless x were a function that returns
a cons cell, that is]
(defun insertion_sort (x y)
(cond
((null x) (cons x y))
((atom x) (cons x y))
((= (car x) y))
((> (car x) y) (cons y x))
((< (car x) y) (insertion_sort (cdr x) y))
)
)
That's actually not a bad first whack at it, but you've got
a few problems. Focus on this: given an element and an
*already sorted* list, return a new list that is still sorted and
contains the element. Write a helper function that does this
and you are halfway to the solution.
I did try it but I didn't see those extra () around x.
Any ways I not sure I quite understand
> Focus on this: given an element and an
> *already sorted* list, return a new list that is still sorted and
> contains the element. Write a helper function that does this
> and you are halfway to the solution.
Isn't that the insertion sort algorith? what I mean is that is what it is
supposed to do return, a sorted list.
hmm.. Im gonna test it out
Joe Marshall <·········@alum.mit.edu> wrote in message
··························@dfw-read.news.verio.net...
>
> Slapstiq <········@rochester.rr.com> wrote in message
> ························@typhoon.nyroc.rr.com...
> > I am trying to write the insertion sort routine this is what I got so
far
> >
> > (defun insertion_sort (x y)
> > (cond
> > ((null x) (cons(x y)))
> > ((atom x) (cons(x y)))
> > ((= (car(x) y) ))
> > ((> (car(x) y)) (cons(y x)))
> > ((< (car(x) y)) (insertion_sort((cdr(x)) y)))
> > )
> > )
> >
> > Is this right?
>
> Did you try it? Did it work?
>
> (You didn't, it won't.)
>
> First, remember Lisp syntax: parentheses are not used
> for grouping, they indicate a function call. So you would
> not write (car (x)) [unless x were a function that returns
> a cons cell, that is]
>
> (defun insertion_sort (x y)
> (cond
> ((null x) (cons x y))
> ((atom x) (cons x y))
> ((= (car x) y))
> ((> (car x) y) (cons y x))
> ((< (car x) y) (insertion_sort (cdr x) y))
> )
> )
>
> That's actually not a bad first whack at it, but you've got
> a few problems. Focus on this: given an element and an
> *already sorted* list, return a new list that is still sorted and
> contains the element. Write a helper function that does this
> and you are halfway to the solution.
>
>
I think I got it - any one see any problems
(defun insertion_sort (x y)
(cond
((null x) (cons y x))
((null y) x )
((= (car x) y) y)
((> (car x) y) (cons y x))
((< (car x) y) (cons (car x) (insertion_sort (cdr x) y)))
)
)
thanks Joe I understood what you meant
Slapstiq
Joe Marshall <·········@alum.mit.edu> wrote in message
··························@dfw-read.news.verio.net...
>
> Slapstiq <········@rochester.rr.com> wrote in message
> ························@typhoon.nyroc.rr.com...
> > I am trying to write the insertion sort routine this is what I got so
far
> >
> > (defun insertion_sort (x y)
> > (cond
> > ((null x) (cons(x y)))
> > ((atom x) (cons(x y)))
> > ((= (car(x) y) ))
> > ((> (car(x) y)) (cons(y x)))
> > ((< (car(x) y)) (insertion_sort((cdr(x)) y)))
> > )
> > )
> >
> > Is this right?
>
> Did you try it? Did it work?
>
> (You didn't, it won't.)
>
> First, remember Lisp syntax: parentheses are not used
> for grouping, they indicate a function call. So you would
> not write (car (x)) [unless x were a function that returns
> a cons cell, that is]
>
> (defun insertion_sort (x y)
> (cond
> ((null x) (cons x y))
> ((atom x) (cons x y))
> ((= (car x) y))
> ((> (car x) y) (cons y x))
> ((< (car x) y) (insertion_sort (cdr x) y))
> )
> )
>
> That's actually not a bad first whack at it, but you've got
> a few problems. Focus on this: given an element and an
> *already sorted* list, return a new list that is still sorted and
> contains the element. Write a helper function that does this
> and you are halfway to the solution.
>
>
"Joe Marshall" <·········@alum.mit.edu> writes:
> Slapstiq <········@rochester.rr.com> wrote in message
> ························@typhoon.nyroc.rr.com...
> > I think I got it - any one see any problems
> >
> > (defun insertion_sort (x y)
> > (cond
> > ((null x) (cons y x))
> > ((null y) x )
> > ((= (car x) y) y)
> > ((> (car x) y) (cons y x))
> > ((< (car x) y) (cons (car x) (insertion_sort (cdr x) y)))
> > )
> > )
> >
> > thanks Joe I understood what you meant
>
> You're welcome. This isn't the whole solution yet, though.
> It only inserts a single element into an already sorted list. Now
> you want to iterate over all the unsorted elements and accumulate
> the sorted result.
>
> Some suggestions:
> Call this helper routine something like `sorted-insert' or
> `insert-one-element'
Another suggestion: use arrays.
--
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - 06 68 10 03 17, fax. +39 - 06 68 80 79 26
http://www.parades.rm.cnr.it/~marcoxa
In article <···················@typhoon.nyroc.rr.com>,
"Slapstiq" <········@rochester.rr.com> writes:
> I am trying to write the insertion sort routine this is what I got so far
>
> (defun insertion_sort (x y)
> (cond
> ((null x) (cons(x y)))
(list y)
> ((atom x) (cons(x y)))
i would assume that the list contains only atoms, so this line looks
somewhat out of place
> ((= (car(x) y) ))
you want to return something, in this case (insertion-sort (cdr x) y)
> ((> (car(x) y)) (cons(y x)))
> ((< (car(x) y)) (insertion_sort((cdr(x)) y)))
> )
> )
>
> Is this right?
it's a start
--
Hartmann Schaffer
It is better to fill your days with life than your life with days