Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Scheme > Re: Sort a list...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 17 Topic 4626 of 4790
Post > Topic >>

Re: Sort a list of dotted pairs

by pjb@[EMAIL PROTECTED] (Pascal J. Bourguignon) Jul 23, 2008 at 07:35 PM

BastiL2001@[EMAIL PROTECTED]
 writes:

> Dear group,
>
> I have a list of dotted pairs that may look something like this:
> ((4 . something) (2 . maybe ) (3 . this ) (7 . last ) (5 . some))
>
> I want it to be sorted after the car of each dotted pair starting with
> the lowest number...
>
> ((2 . maybe) (3 . this) (4 . something) (5 . some) (7 . last))
>
> Only thing I have done so far is the sort function which does not seem
> to help in that case... How could this be done? Thanks.

It depends on how you made this sort function.
If you made it like Common Lisp sort, then you can just write:

     (sort a-list < 'key car)

But if you made it like the sort we usually find in scheme libraries,
then  you will have to write:

(define (identity x)
   x)

(define (get plist key . default)
   (cond
      ((or (null? plist) (null? (cdr plist)))
         (if (pair? default)
            (car default)
            #f))
      ((eqv? key (car plist))
         (cadr plist))
      (else
          (get (cddr plist) key (if (pair? default)
                                    (car default)
                                    #f)))))
       

(define (cl-sort list less? . options)
   (if (null? options)
      (scheme-sort list less?)
      (let ((key (get options 'key identity)))
         (scheme-sort list (lambda (a b) (less? (key a) (key b)))))))


(cl-sort '((4 . something) (2 . maybe ) (3 . this ) (7 . last ) (5 .
some))
         <
         'key car)

--> ((2 . maybe) (3 . this) (4 . something) (5 . some) (7 . last))



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This is a signature virus.  Add me to your signature and help me to live.
 




 17 Posts in Topic:
Sort a list of dotted pairs
BastiL2001@[EMAIL PROTECT  2008-07-23 08:20:08 
Re: Sort a list of dotted pairs
Grant Rettke <grettke@  2008-07-23 10:05:04 
Re: Sort a list of dotted pairs
pjb@[EMAIL PROTECTED] (P  2008-07-23 19:35:13 
Re: Sort a list of dotted pairs
Abdulaziz Ghuloum <agh  2008-07-23 11:51:56 
Re: Sort a list of dotted pairs
"Kjetil S. Matheusse  2008-07-24 10:25:16 
Re: Sort a list of dotted pairs
"Kjetil S. Matheusse  2008-07-24 12:59:28 
Re: Sort a list of dotted pairs
pjb@[EMAIL PROTECTED] (P  2008-07-24 14:36:45 
Re: Sort a list of dotted pairs
leppie <xacc.ide@[EMAI  2008-07-24 05:52:43 
Re: Sort a list of dotted pairs
leppie <xacc.ide@[EMAI  2008-07-24 05:55:43 
Re: Sort a list of dotted pairs
Grant Rettke <grettke@  2008-07-24 06:29:16 
Re: Sort a list of dotted pairs
"Kjetil S. Matheusse  2008-07-24 16:24:53 
Re: Sort a list of dotted pairs
pjb@[EMAIL PROTECTED] (P  2008-07-24 16:27:20 
Re: Sort a list of dotted pairs
"Kjetil S. Matheusse  2008-07-24 16:25:24 
Re: Sort a list of dotted pairs
leppie <xacc.ide@[EMAI  2008-07-24 07:46:52 
Re: Sort a list of dotted pairs
leppie <xacc.ide@[EMAI  2008-07-24 07:51:28 
Re: Sort a list of dotted pairs
Grant Rettke <grettke@  2008-07-24 08:43:25 
Re: Sort a list of dotted pairs
"Kjetil S. Matheusse  2008-07-24 18:09:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 9:02:56 CDT 2008.