From: Kalamaro
Subject: Sort letters
Date: 
Message-ID: <al83g0$1oj7bm$1@ID-115330.news.dfncis.de>
Hi. How can i make a function that cons 2 lists and sort the numbers, and 
the letters (if they are not of type 'char') like this?:

Input:
(function '(4 p 2 5) '(m 6 a v e))  
Output:
(2 4 5 6) (a e m p v)

I've tried to use sort which works fine with numbers but i get an error 
message with letters about the type. Do I have to assign a value to the 
letters variables?

thanks

From: Christopher Browne
Subject: Re: Sort letters
Date: 
Message-ID: <al8b15$1ntdua$3@ID-125932.news.dfncis.de>
Oops! Kalamaro <········@usuarios.retecal.es> was seen spray-painting on a wall:
> Hi. How can i make a function that cons 2 lists and sort the numbers, and 
> the letters (if they are not of type 'char') like this?:
>
> Input:
> (function '(4 p 2 5) '(m 6 a v e))  
> Output:
> (2 4 5 6) (a e m p v)
>
> I've tried to use sort which works fine with numbers but i get an error 
> message with letters about the type. Do I have to assign a value to the 
> letters variables?

Seems to work perfectly well with characters.

(let ((a '(4 #\p 2 5))
      (b '(#\m 6 #\a #\v #\e))
      (letters nil)
      (numbers nil))
  (loop for i in (append a b)
	when (characterp i)
	do (push i letters)
	when (numberp i)
	do (push i numbers))
  (list (sort letters #'char< ) (sort numbers #'<)))

If you want to compare symbol names, you might want to make up a
comparison function that compares the names of symbols.  SYMBOL-NAME
might be helpful in this task...
-- 
(concatenate 'string "chris" ·@cbbrowne.com")
http://www.ntlug.org/~cbbrowne/rdbms.html
"If there are aliens, they use Lisp."
--Edward Lasker, mutatis mutandi
From: Nils Goesche
Subject: Re: Sort letters
Date: 
Message-ID: <lk1y88dw0p.fsf@pc022.bln.elmeg.de>
Christopher Browne <········@acm.org> writes:

> If you want to compare symbol names, you might want to make up a
> comparison function that compares the names of symbols.  SYMBOL-NAME
> might be helpful in this task...

Actually, you can use STRING< for characters, symbols and strings.

CL-USER 1 > (sort (list #\b 'a "c") #'string<)
(A #\b "c")

It takes ``string designators'' as arguments.

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Johan Ur Riise
Subject: Re: Sort letters
Date: 
Message-ID: <87wuq085o7.fsf@dill.topp.dyndns.com>
Nils Goesche <······@cartan.de> writes:

> Christopher Browne <········@acm.org> writes:

> Actually, you can use STRING< for characters, symbols and strings.

(defun sortit (diddel dudel)
  (loop for elem in (append diddel dudel)
	if (numberp elem) collect elem into numbers
	else collect elem into alfas
	finally (return (values (sort numbers #'<) (sort alfas #'string<)))))

(sortit '(4 p 2 5) '(m 6 a v e))  
=>
(2 4 5 6)
(A E M P V)
From: Kalamaro
Subject: Re: Sort letters
Date: 
Message-ID: <al8m3m$1o2raa$1@ID-115330.news.dfncis.de>
Thanks a lot, Nils, Christopher and Johan for your answers.
From: Johan Ur Riise
Subject: Re: Sort letters
Date: 
Message-ID: <878z2fwhjw.fsf@dill.topp.dyndns.com>
Kalamaro <········@usuarios.retecal.es> writes:

> Thanks a lot, Nils, Christopher and Johan for your answers.

Two lines shorter:

(defun sortit (diddel dudel)
  (values (sort (remove-if-not #'numberp (append diddel dudel)) #'<)
	  (sort (remove-if #'numberp (append diddel dudel)) #'string<)))
From: Espen Wiborg
Subject: Re: Sort letters
Date: 
Message-ID: <u4rd3qszm.fsf@grumblesmurf.org>
Johan Ur Riise <·@rsc.no> writes:
> Kalamaro <········@usuarios.retecal.es> writes:
> > Thanks a lot, Nils, Christopher and Johan for your answers.
> Two lines shorter:
> (defun sortit (diddel dudel)
>   (values (sort (remove-if-not #'numberp (append diddel dudel)) #'<)
> 	  (sort (remove-if #'numberp (append diddel dudel)) #'string<)))

This will do the append twice.  The solutions using loop are more
efficient - or you could do this:

(defun sortit (foo bar)
  (let ((zot (append foo bar)))
    (values (sort (remove-if-not #'numberp zot) #'<)
            (sort (remove-if #'numberp zot) #'string<))))

-- 
Espen Wiborg <·······@grumblesmurf.org>
--Fast
--Secure
--Reliable
Pick two.
From: Edi Weitz
Subject: Re: Sort letters
Date: 
Message-ID: <8765xjpddg.fsf@bird.agharta.de>
Espen Wiborg <·······@grumblesmurf.org> writes:

> Johan Ur Riise <·@rsc.no> writes:
> > Kalamaro <········@usuarios.retecal.es> writes:
> > > Thanks a lot, Nils, Christopher and Johan for your answers.
> > Two lines shorter:
> > (defun sortit (diddel dudel)
> >   (values (sort (remove-if-not #'numberp (append diddel dudel)) #'<)
> > 	  (sort (remove-if #'numberp (append diddel dudel)) #'string<)))
> 
> This will do the append twice.  The solutions using loop are more
> efficient - or you could do this:
> 
> (defun sortit (foo bar)
>   (let ((zot (append foo bar)))
>     (values (sort (remove-if-not #'numberp zot) #'<)
>             (sort (remove-if #'numberp zot) #'string<))))

which traverses zot twice. The solution with LOOP is still more
efficient.

Edi.
From: Espen Wiborg
Subject: Re: Sort letters
Date: 
Message-ID: <uznuvpd2o.fsf@grumblesmurf.org>
Edi Weitz <···@agharta.de> writes:
> Espen Wiborg <·······@grumblesmurf.org> writes:
> > Johan Ur Riise <·@rsc.no> writes:
> > > Kalamaro <········@usuarios.retecal.es> writes:
> > > > Thanks a lot, Nils, Christopher and Johan for your answers.
> > > Two lines shorter:
<-snip->
> > This will do the append twice.  The solutions using loop are more
> > efficient - or you could do this:
<-snip->
> which traverses zot twice. The solution with LOOP is still more
> efficient.

As I wrote - although I realize that my use of 'or' is a bit
ambiguous.  I should probably have written something like "[...] - or,
if you want to do this the 'functional programming' way, you could do
this, which is still not optimal:".

Achieving precision in language is rarely easy.  :)

-- 
Espen Wiborg <·······@grumblesmurf.org>
The average person's left hand does 56% of the typing.
From: Nils Goesche
Subject: Re: Sort letters
Date: 
Message-ID: <lk65xke4bu.fsf@pc022.bln.elmeg.de>
Kalamaro <········@usuarios.retecal.es> writes:

> How can i make a function that cons 2 lists and sort the numbers,
> and the letters (if they are not of type 'char') like this?:
> 
> Input:
> (function '(4 p 2 5) '(m 6 a v e))  

Note that the FUNCTION special operator already has a defined meaning ;-)

> Output:
> (2 4 5 6) (a e m p v)
> 
> I've tried to use sort which works fine with numbers but i get an error 
> message with letters about the type. Do I have to assign a value to the 
> letters variables?

Lookup CHAR< and STRING<.  Do you really want to represent your
``letters'' as symbols?  Why not characters?

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0
From: Software Scavenger
Subject: Re: Sort letters
Date: 
Message-ID: <a6789134.0209051514.50ad1db0@posting.google.com>
Nils Goesche <······@cartan.de> wrote in message news:<··············@pc022.bln.elmeg.de>...

> Lookup CHAR< and STRING<.  Do you really want to represent your
> ``letters'' as symbols?  Why not characters?

What's wrong with symbols?  (string< 'a 'b) works fine.
And '(a b c d e) is easier to read and write than
(#\A #\B #\C #\D #\E)
From: Nils Goesche
Subject: Re: Sort letters
Date: 
Message-ID: <lksn0nclxc.fsf@pc022.bln.elmeg.de>
··········@mailandnews.com (Software Scavenger) writes:

> Nils Goesche <······@cartan.de> wrote in message news:<··············@pc022.bln.elmeg.de>...
> 
> > Lookup CHAR< and STRING<.  Do you really want to represent your
> > ``letters'' as symbols?  Why not characters?
> 
> What's wrong with symbols?

Well, nothing.  I don't know what the OP is trying to do.  Sometimes
using characters is better.  If he was writing a lexer by popping
characters off a text stream and converting them to symbols and intern
the individual characters, I'd tell him to do something different, for
instance.

Regards,
-- 
Nils Goesche
"Don't ask for whom the <CTRL-G> tolls."

PGP key ID 0x0655CFA0