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
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
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
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)
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.
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.
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.
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
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)
··········@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