Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Char ordering.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 66 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jacek Generowicz  
View profile  
 More options Mar 8 2002, 5:12 am
Newsgroups: comp.lang.lisp
From: Jacek Generowicz <jacek.generow...@cern.ch>
Date: 08 Mar 2002 10:57:27 +0100
Local: Fri, Mar 8 2002 4:57 am
Subject: Char ordering.
I want to write a function which converts the letters a-z (lower or
uppercase) to the integer 0; f,g,h,j,k (note absence of i) to the
integer 1; l-p to 2; q-u to 3 and v-z to 4.

Ignoring the annoyance of the missing i, a floor operation, and an upcase, this
boils down to mapping the letters to consecutive numbers.

The spec tells me that partial ordering of characters is guaranteed,
but points out that contiguity is not.

How should I go about writing this function in an implementation
independent way ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Antoniotti  
View profile  
 More options Mar 8 2002, 10:43 am
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: 08 Mar 2002 10:43:19 -0500
Local: Fri, Mar 8 2002 10:43 am
Subject: Re: Char ordering.

Jacek Generowicz <jacek.generow...@cern.ch> writes:
> I want to write a function which converts the letters a-z (lower or
> uppercase) to the integer 0; f,g,h,j,k (note absence of i) to the
> integer 1; l-p to 2; q-u to 3 and v-z to 4.

> Ignoring the annoyance of the missing i, a floor operation, and an
> upcase, this boils down to mapping the letters to consecutive numbers.

> The spec tells me that partial ordering of characters is guaranteed,
> but points out that contiguity is not.

> How should I go about writing this function in an implementation
> independent way ?

I assume you mean

(range #\a #\e)  => 0
(range #\f #\h)  => 1
(set-of #\j #\k) => 1
(range #\l #\p)  => 2
(range #\q #\u)  => 3
(range #\v #\z)  => 4

(range #\A #\E)  => 0
(range #\F #\H)  => 1
(set-of #\J #\K) => 1
(range #\L #\P)  => 2
(range #\Q #\U)  => 3
(range #\V #\Z)  => 4

=========================================================================== ===
(defmethod in-test ((item character) (op (eql 'set-of)) &rest spec)
  (member item spec :test #'char=))

(defmethod in-test ((item character) (op (eql 'range)) &rest spec)
  (char<= (first spec) item (second spec)))

(defmacro in (c spec)
  `(in-test ,c ',(first spec) ,@(rest spec)))

(defun funky-test (c)
  (cond ((in c (range #\a #\e)) 0)
        ((in c (range #\a #\e)) 0)
        ((in c (range #\f #\h)) 1)
        ((in c (set-of #\j #\k)) 1)
        ((in c (range #\l #\p)) 2)
        ((in c (range #\q #\u)) 3)
        ((in c (range #\v #\z)) 4)

        ((in c (range #\A #\E)) 0)
        ((in c (range #\F #\H)) 1)
        ((in c (set-of #\J #\K)) 1)
        ((in c (range #\L #\P)) 2)
        ((in c (range #\Q #\U)) 3)
        ((in c (range #\V #\Z)) 4)

        (t ; I.e. `(in c (set-of #\i #\I))
          (error "Got an ~C." c))
        ))
=========================================================================== ===

Untested.  Probably you can do it in an easier and/or compact and faster ways
(note that `(not (eq 'compact 'easier))'.  But this example
exercises the language in several nice ways.

Cheers

--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Mar 8 2002, 11:16 am
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Fri, 08 Mar 2002 15:47:12 GMT
Local: Fri, Mar 8 2002 10:47 am
Subject: Re: Char ordering.
In article <tyfvgc75qm0....@pcitapi22.cern.ch>,
Jacek Generowicz  <jacek.generow...@cern.ch> wrote:

>I want to write a function which converts the letters a-z (lower or
>uppercase) to the integer 0; f,g,h,j,k (note absence of i) to the
>integer 1; l-p to 2; q-u to 3 and v-z to 4.

Did you mean to say a-e goes to 0?

>Ignoring the annoyance of the missing i, a floor operation, and an upcase, this
>boils down to mapping the letters to consecutive numbers.

>The spec tells me that partial ordering of characters is guaranteed,
>but points out that contiguity is not.

>How should I go about writing this function in an implementation
>independent way ?

Sounds like a COND statement containing char>= and char<= predicates would
do it:

(cond ((char<= #\a letter #\e) 0)
      ...)

Another possibility is filling in a hash table with all the mappings.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacek Generowicz  
View profile  
 More options Mar 8 2002, 11:42 am
Newsgroups: comp.lang.lisp
From: Jacek Generowicz <jacek.generow...@cern.ch>
Date: 08 Mar 2002 17:37:26 +0100
Local: Fri, Mar 8 2002 11:37 am
Subject: Re: Char ordering.

Yup.

Food for thought there, thank you.

If my aim were clarity and conciseness rather than exercising the
language I guess I might develop your idea thus:

(defun funky-test (c)
  (let ((upc (char-upcase c)))
    (cond ((char<= #\A upc #\E) 0)
          ((char<= #\F upc #\H) 1)
          ((char<= #\J upc #\K) 1)
          ((char<= #\L upc #\P) 2)
          ((char<= #\Q upc #\U) 3)
          ((char<= #\V upc #\Z) 4)
          (t (error "I don't like ~c !" c)))))

(map `list #'funky-test "abcdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ")

=>

(0 0 0 0 0
 1 1 1 1 1
 2 2 2 2 2
 3 3 3 3 3
 4 4 4 4 4
 0 0 0 0 0
 1 1 1 1 1
 2 2 2 2 2
 3 3 3 3 3
 4 4 4 4 4)

As it turns out, I also need a second set of values. Imagine the
letters appear on a grid:

    V W X Y Z
    Q R S T U
    L M N O P
    F G H J K
    A B C D E

and we are looking for _both_ coordinates. Getting the x coordinate in
this way looks like a lot more hassle, and thus the idea of assigning
consecutive values to the letters (omitting i) and using floor is
appealing.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Mar 8 2002, 11:47 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: Fri, 08 Mar 2002 16:47:57 GMT
Local: Fri, Mar 8 2002 11:47 am
Subject: Re: Char ordering.
* Jacek Generowicz <jacek.generow...@cern.ch>
| I want to write a function which converts the letters a-z (lower or
| uppercase) to the integer 0; f,g,h,j,k (note absence of i) to the
| integer 1; l-p to 2; q-u to 3 and v-z to 4.
|
| Ignoring the annoyance of the missing i, a floor operation, and an
| upcase, this boils down to mapping the letters to consecutive numbers.

  Huh?

| The spec tells me that partial ordering of characters is guaranteed, but
| points out that contiguity is not.

  So?  Who cares?

| How should I go about writing this function in an implementation
| independent way ?

(case (char-upcase char)
  ((#\A #\B #\C #\D #\E) 0)
  ((#\F #\G #\H #\J #\K) 1)
  ((#\l #\m #\n #\o #\p) 2)
  ((#\Q #\R #\S #\T #\U) 3)
  ((#\V #\W #\X #\Y #\Z) 4))

///
--
  In a fight against something, the fight has value, victory has none.
  In a fight for something, the fight is a loss, victory merely relief.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacek Generowicz  
View profile  
 More options Mar 8 2002, 12:12 pm
Newsgroups: comp.lang.lisp
From: Jacek Generowicz <jacek.generow...@cern.ch>
Date: 08 Mar 2002 17:45:15 +0100
Local: Fri, Mar 8 2002 11:45 am
Subject: Re: Char ordering.

Barry Margolin <bar...@genuity.net> writes:
> In article <tyfvgc75qm0....@pcitapi22.cern.ch>,
> Jacek Generowicz  <jacek.generow...@cern.ch> wrote:
> >I want to write a function which converts the letters a-z (lower or
> >uppercase) to the integer 0; f,g,h,j,k (note absence of i) to the
> >integer 1; l-p to 2; q-u to 3 and v-z to 4.

> Did you mean to say a-e goes to 0?

I did. Sorry.

> >Ignoring the annoyance of the missing i, a floor operation, and an
> >upcase, this boils down to mapping the letters to consecutive
> >numbers.

> >The spec tells me that partial ordering of characters is guaranteed,
> >but points out that contiguity is not.

> >How should I go about writing this function in an implementation
> >independent way ?

> Sounds like a COND statement containing char>= and char<= predicates
> would do it:

> (cond ((char<= #\a letter #\e) 0)
>       ...)

Yes, see my reply to Marco for a better and fuller description of the
problem, which shows why this has its problems.

> Another possibility is filling in a hash table with all the mappings.

Yes . . . but I was hoping to find a more interesting solution.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacek Generowicz  
View profile  
 More options Mar 8 2002, 12:12 pm
Newsgroups: comp.lang.lisp
From: Jacek Generowicz <jacek.generow...@cern.ch>
Date: 08 Mar 2002 18:00:46 +0100
Local: Fri, Mar 8 2002 12:00 pm
Subject: Re: Char ordering.

Erik Naggum <e...@naggum.net> writes:
> (case (char-upcase char)
>   ((#\A #\B #\C #\D #\E) 0)
>   ((#\F #\G #\H #\J #\K) 1)
>   ((#\l #\m #\n #\o #\p) 2)
>   ((#\Q #\R #\S #\T #\U) 3)
>   ((#\V #\W #\X #\Y #\Z) 4))

Hmm.

I guess I can't claim that this is going to become too tedious or
error-prone to write for, err, much longer alphabets :-)

Having said that, the fact that the approach _is_ error-prone is
demonstrated by the line of lowercase chars in your code.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Mar 8 2002, 12:18 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Fri, 08 Mar 2002 17:05:36 GMT
Local: Fri, Mar 8 2002 12:05 pm
Subject: Re: Char ordering.
In article <tyfr8mv3tix....@pcitapi22.cern.ch>,
Jacek Generowicz  <jacek.generow...@cern.ch> wrote:

>As it turns out, I also need a second set of values. Imagine the
>letters appear on a grid:

>    V W X Y Z
>    Q R S T U
>    L M N O P
>    F G H J K
>    A B C D E

>and we are looking for _both_ coordinates. Getting the x coordinate in
>this way looks like a lot more hassle, and thus the idea of assigning
>consecutive values to the letters (omitting i) and using floor is
>appealing.

Turn the grid into a vector:

(defconstant +grid-vector+ "AFLQVBGMRWCHNSXDJOTYEKPUZ")

(defun char-x-coord (char)
  (let* ((upc (char-upcase char))
         (pos (position upc +grid-vector+)))
    (when pos
      (floor pos 5))))

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Wade Humeniuk  
View profile  
 More options Mar 8 2002, 12:25 pm
Newsgroups: comp.lang.lisp
From: "Wade Humeniuk" <humen...@cadvision.com>
Date: Fri, 8 Mar 2002 10:30:47 -0700
Local: Fri, Mar 8 2002 12:30 pm
Subject: Re: Char ordering.
"Jacek Generowicz" <jacek.generow...@cern.ch> wrote in message

news:tyfadtj3sg1.fsf@pcitapi22.cern.ch...

Take the solution Jacek.  Its the best one.  Why make your life difficult?
All coding is error prone.

Wade


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Bushnell, BSG  
View profile  
 More options Mar 8 2002, 12:40 pm
Newsgroups: comp.lang.lisp
From: tb+use...@becket.net (Thomas Bushnell, BSG)
Date: 08 Mar 2002 09:40:30 -0800
Local: Fri, Mar 8 2002 12:40 pm
Subject: Re: Char ordering.

And the lack of one letter too.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Mar 8 2002, 12:47 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Fri, 08 Mar 2002 17:47:26 GMT
Local: Fri, Mar 8 2002 12:47 pm
Subject: Re: Char ordering.
In article <a6as70$tb...@news3.cadvision.com>,

Actually, there's a solution that gets both his X and Y coordinates easily:

(defconstant +grid-letters+ "ABCDEFGHJKLMNOPQRSTUVWXYZ

(defun char-coords (char)
  "Returns two values: X and Y coordinates of CHAR in the letter grid."
  (set char (char-upcase char))
  (let ((pos (position char +grid-letters+)))
    (unless pos
      (error "CHAR ~S is not in the grid." char))
    ;; Our values are the opposite order of FLOORs
    (multiple-value-bind (quotient remainder) (floor pos 5)
      (values remainder quotient))))

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacek Generowicz  
View profile  
 More options Mar 8 2002, 1:12 pm
Newsgroups: comp.lang.lisp
From: Jacek Generowicz <jacek.generow...@cern.ch>
Date: 08 Mar 2002 19:00:16 +0100
Local: Fri, Mar 8 2002 1:00 pm
Subject: Re: Char ordering.
tb+use...@becket.net (Thomas Bushnell, BSG) writes:

If you are referring to #\I . . . it was part of the spec that it
should be missing.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacek Generowicz  
View profile  
 More options Mar 8 2002, 1:42 pm
Newsgroups: comp.lang.lisp
From: Jacek Generowicz <jacek.generow...@cern.ch>
Date: 08 Mar 2002 19:40:23 +0100
Local: Fri, Mar 8 2002 1:40 pm
Subject: Re: Char ordering.

Because I might learn something in the process ?

> >All coding is error prone.

That's no reason not to try to reduce the likelyhood of errors.

> Actually, there's a solution that gets both his X and Y coordinates easily:

> (defconstant +grid-letters+ "ABCDEFGHJKLMNOPQRSTUVWXYZ

> (defun char-coords (char)
>   "Returns two values: X and Y coordinates of CHAR in the letter grid."
>   (set char (char-upcase char))
>   (let ((pos (position char +grid-letters+)))
>     (unless pos
>       (error "CHAR ~S is not in the grid." char))
>     ;; Our values are the opposite order of FLOORs
>     (multiple-value-bind (quotient remainder) (floor pos 5)
>       (values remainder quotient))))

Yes, I didn't like the dodgy order of the letters in the vector (in
your previous version) either.

I also didn't tell the _whole_ truth about the position of the letters
in the grid. (See below.)

In case you are wordering who the hell came up with this grid, it's
the British Ordnance Survey.

So, here are the two candidate solutions. I'm not sure which one is
cleaner. Maybe because of the faffing around necessary to invert the
order in no. 1, no. 2 is ends up more transparent.

Anyway, thanks for the ideas.

(defconstant +grid-vector+ "ABCDEFGHJKLMNOPQRSTUVWXYZ")
(defun grid-letter-to-position-1 (letter)
  "Converts a single letter into a pair of values representing the
letter's position in a 5x5 grid of letters:
   A B C D E    04 14 24 34 44
   F G H J K    03 13 23 33 43
   L M N O P    02 12 22 32 42
   Q R S T U    01 11 21 31 41
   V W X Y Z    00 10 20 30 40"
  (let* ((upcased-letter (char-upcase letter))
         (pos (position upcased-letter +grid-vector+)))
    (unless pos
      (error "The character ~S is not valid in a sheet name." letter))
    (multiple-value-bind (south east) (floor pos 5)
      (values east (- 4 south)))))

(defun grid-letter-to-position-2 (letter)
  "Converts a single letter into a pair of values representing the
letter's position in a 5x5 grid of letters:
   A B C D E    04 14 24 34 44
   F G H J K    03 13 23 33 43
   L M N O P    02 12 22 32 42
   Q R S T U    01 11 21 31 41
   V W X Y Z    00 10 20 30 40"
  (let ((upcased-letter (char-upcase letter)))
    (values
     (ecase upcased-letter
       ((#\A #\F #\L #\Q #\V) 0)
       ((#\B #\G #\M #\R #\W) 1)
       ((#\C #\H #\N #\S #\X) 2)
       ((#\D #\J #\O #\T #\Y) 3)
       ((#\E #\K #\P #\U #\Z) 4))
     (ecase upcased-letter
       ((#\A #\B #\C #\D #\E) 4)
       ((#\F #\G #\H #\J #\K) 3)
       ((#\L #\M #\N #\O #\P) 2)
       ((#\Q #\R #\S #\T #\U) 1)
       ((#\V #\W #\X #\Y #\Z) 0)))))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Mar 8 2002, 2:18 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Fri, 08 Mar 2002 17:49:11 GMT
Local: Fri, Mar 8 2002 12:49 pm
Subject: Re: Char ordering.
In article <87sn7b6jqp....@becket.becket.net>,
Thomas Bushnell, BSG <tb+use...@becket.net> wrote:

Which letter is missing?  #\I is *supposed* to be skipped; his application
finds the location of the letter in a 5x5 grid, and you can't put all 26
letters of the alphabet in a 25-element grid.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Mar 8 2002, 2:20 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: Fri, 08 Mar 2002 19:20:11 GMT
Local: Fri, Mar 8 2002 2:20 pm
Subject: Re: Char ordering.
* Jacek Generowicz <jacek.generow...@cern.ch>
| I guess I can't claim that this is going to become too tedious or
| error-prone to write for, err, much longer alphabets :-)

  I "wrote" it using Emacs Lisp.  For each line I evaluated

(loop for i from ?V to ?Z do (insert (format "#\\%c " i)))

  You could have accomplished the same thing with #., but I think the code
  looks a lot better this way.

  Unfortunately, I typed in lower-case letters in one of the lines.

| Having said that, the fact that the approach _is_ error-prone is
| demonstrated by the line of lowercase chars in your code.

  I have added you to my do-not-help list.

///
--
  In a fight against something, the fight has value, victory has none.
  In a fight for something, the fight is a loss, victory merely relief.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Mar 8 2002, 2:22 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: Fri, 08 Mar 2002 19:22:54 GMT
Local: Fri, Mar 8 2002 2:22 pm
Subject: Re: Char ordering.
* Thomas Bushnell, BSG
| And the lack of one letter too.

  The specification of the goddamn _problem_ required I to be excluded, you
  obnoxious dimwit.  Pay some _attention_ around here, will you?

///
--
  In a fight against something, the fight has value, victory has none.
  In a fight for something, the fight is a loss, victory merely relief.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Long  
View profile  
 More options Mar 8 2002, 2:40 pm
Newsgroups: comp.lang.lisp
From: Steve Long <sal6...@hotmail.com>
Date: Thu, 07 Mar 2002 23:59:42 -0800
Local: Fri, Mar 8 2002 2:59 am
Subject: Re: Char ordering.
Jacek,

There is nothing error-prone about this. He just made a typo; you aren't
going to get anything tighter than a CASE statement like this.

Alas, like any source code, it is subject to the failings of our
fingers. Then again, he might have done this on purpose for educational
purposes, to see if you were a student looking for homework assistance
(who can tell?)

Your character sets can't get any longer than what's available on your
system (255?). You can always right a function or macro around his idea
to handle expressions of of character intervals (regular expression
patterns like [A-G,a-g] that expand into a CASE statement with very
large keys.

slong


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Long  
View profile  
 More options Mar 8 2002, 2:54 pm
Newsgroups: comp.lang.lisp
From: Steve Long <sal6...@hotmail.com>
Date: Fri, 08 Mar 2002 00:14:01 -0800
Local: Fri, Mar 8 2002 3:14 am
Subject: Re: Char ordering.
Looks like you've solved it. They are both sound solutions; unless this is a
library routine, the differences in memory and execution time cost between the
two is probably not important.

slong


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Mar 8 2002, 3:07 pm
Newsgroups: comp.lang.lisp
From: Barry Margolin <bar...@genuity.net>
Date: Fri, 08 Mar 2002 20:07:36 GMT
Local: Fri, Mar 8 2002 3:07 pm
Subject: Re: Char ordering.
In article <3C886F6D.6AF79...@hotmail.com>,
Steve Long  <sal6...@hotmail.com> wrote:

>Jacek,

>There is nothing error-prone about this. He just made a typo; you aren't
>going to get anything tighter than a CASE statement like this.

I think that the OP was hoping that there were some built-in way to map
letters directly to their position in the alphabet.  If the codes were
required to be contiguous, you could do something like:

(- (char-code upc) (char-code #\A))

The reason that this is not specified is because we didn't want to tie
Common Lisp to specific character encodings.  In particular, I believe
EBCDIC does not have the property that the letters have sequential values,
the way they do in ASCII.  All well-known character encodings follow the
partial ordering that Common Lisp specifies for char-code, but anything
more specific would probably rule out some well-known encodings.

Note also that CL provides the DIGIT-CHAR function.  In other programming
languages it's common to assume the decimal digits are sequential and do
something analogous to:

(code-char (+ (char-code #\0) digit))

In an implementation that uses ASCII, this is likely to be what the
definition of DIGIT-CHAR looks like (there will also be code to handle
radix > 10), but the application programmer doesn't have to deal with it.
Siilarly, this is the reason why DIGIT-CHAR-P returns the digit's numeric
value.

--
Barry Margolin, bar...@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kent M Pitman  
View profile  
 More options Mar 8 2002, 3:08 pm
Newsgroups: comp.lang.lisp
From: Kent M Pitman <pit...@world.std.com>
Date: Fri, 8 Mar 2002 20:07:41 GMT
Local: Fri, Mar 8 2002 3:07 pm
Subject: Re: Char ordering.

Steve Long <sal6...@hotmail.com> writes:
> Jacek,

> There is nothing error-prone about this. He just made a typo; you aren't
> going to get anything tighter than a CASE statement like this.

You're not? How about

(let ((code (char-code ch)))
  (if (> code 128) -1
      (svref #.(let ((array (make-array 128 :initial-element -1)))
                 (loop for ch
                        across "abcdefghjklmnopqrstuvwxyz" ;apparently no "i"
                       for lc = (char-code ch)
                       for uc = (char-code (char-upcase ch))
                       for i from 0
                       do ;; I made both upper and lowercase work here, but
                          ;; obviously you could choose one or the other.
                          (setf (aref array lc)
                                (setf (aref array uc)
                                      (truncate i 5)))))
             i)))

In most implementations, I'd expect this to be faster than the CASE
statement, not that I checked.  At least if you're doing straight
ordering, since it's 25 compares in the worst case and 12.5 in the
average case, compared to the cost of one char-code and one SVREF.

In certain pathological data where you had a very biased distribution
and you exploited that in the case ordering, you might do better with
the CASE, though I bet even then you'd need nested cases to get
optimal speed.

> > > (case (char-upcase char)
> > >   ((#\A #\B #\C #\D #\E) 0)
> > >   ((#\F #\G #\H #\J #\K) 1)
> > >   ((#\l #\m #\n #\o #\p) 2)
> > >   ((#\Q #\R #\S #\T #\U) 3)
> > >   ((#\V #\W #\X #\Y #\Z) 4))

Disclaimer: No, I didn't test my code.  Left as an exercise to anyone who
            cares. But you get the idea.

And, obviously, this could be abstracted if you did it a lot.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marco Antoniotti  
View profile  
 More options Mar 8 2002, 4:35 pm
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: 08 Mar 2002 16:35:41 -0500
Local: Fri, Mar 8 2002 4:35 pm
Subject: Re: Char ordering.

Exactly. One of the Pascal programs in "Algorithms + Data Strucutres =
Programs" by Wirth had exactly that property.  It assumed a character
ordering /= ASCII.

Cheers
--
Marco Antoniotti ========================================================
NYU Courant Bioinformatics Group        tel. +1 - 212 - 998 3488
719 Broadway 12th Floor                 fax  +1 - 212 - 995 4122
New York, NY 10003, USA                 http://bioinformatics.cat.nyu.edu
                    "Hello New York! We'll do what we can!"
                           Bill Murray in `Ghostbusters'.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Bushnell, BSG  
View profile  
 More options Mar 8 2002, 5:00 pm
Newsgroups: comp.lang.lisp
From: tb+use...@becket.net (Thomas Bushnell, BSG)
Date: 08 Mar 2002 13:54:53 -0800
Local: Fri, Mar 8 2002 4:54 pm
Subject: Re: Char ordering.

Jacek Generowicz <jacek.generow...@cern.ch> writes:
> If you are referring to #\I . . . it was part of the spec that it
> should be missing.

Whoops, my mistake.  ::blush::

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Bushnell, BSG  
View profile  
 More options Mar 8 2002, 5:00 pm
Newsgroups: comp.lang.lisp
From: tb+use...@becket.net (Thomas Bushnell, BSG)
Date: 08 Mar 2002 13:56:38 -0800
Local: Fri, Mar 8 2002 4:56 pm
Subject: Re: Char ordering.

Erik Naggum <e...@naggum.net> writes:
>   The specification of the goddamn _problem_ required I to be excluded, you
>   obnoxious dimwit.  Pay some _attention_ around here, will you?

I know you have the benefit of never making mistakes, but I do make
them sometimes.  Oh, actually, your code did have a mistake, but that
apparently doesn't count as one.

I already acknowledged my error (kindly pointed out by people who
actually have social skills).  I only post this message to add the
comment that many more insults and my news reader will be
automatically scoring your posts so low that I don't see them; if
messages of yours seem to call for a response but don't get one, that
will be why.

Thomas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Naggum  
View profile  
 More options Mar 8 2002, 9:29 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.net>
Date: Sat, 09 Mar 2002 02:29:08 GMT
Local: Fri, Mar 8 2002 9:29 pm
Subject: Re: Char ordering.
* Thomas Bushnell, BSG
| I know you have the benefit of never making mistakes,

  Let me know when you have cooled down so you do not make or at least post
  this kind of stupid insults.  FYI: When I make mistakes, I am not averse
  to being corrected at all, in whatever way it seems appropriate, but I
  when some fault-finding asshole finds faults that are not even there, I
  conclude that it can only be malicious because it takes so little effort
  to pay sufficient attention to avoid making the mistake of accusing
  others of making mistakes they have not made and if you take the trouble
  to post a correction, you better be damn sure it is _more_ accurate than
  what you correct.  It this really too much ask?  Is it really productive
  for those who mistakenly "correct" others to try to defend themselves by
  attacking the one they have false accused when they should instead
  apologize for carelessly jumping to wrong conclusions.  Returning insults
  is pretty solid evidence that the false accusation _was_ malicious.

///
--
  In a fight against something, the fight has value, victory has none.
  In a fight for something, the fight is a loss, victory merely relief.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christopher Browne  
View profile  
 More options Mar 8 2002, 9:38 pm
Newsgroups: comp.lang.lisp
From: Christopher Browne <cbbro...@acm.org>
Date: Fri, 08 Mar 2002 21:37:39 -0500
Local: Fri, Mar 8 2002 9:37 pm
Subject: Re: Char ordering.
Centuries ago, Nostradamus foresaw when Erik Naggum <e...@naggum.net> would write:

> * Thomas Bushnell, BSG
> | And the lack of one letter too.
> The specification of the goddamn _problem_ required I to be
> excluded, you obnoxious dimwit.  Pay some _attention_ around here,
> will you?

It was excluded from the "values mapping to 1" sequence.

The very first clause of the spec said a-z map to 0, so for I to map
to NIL seems somewhat unexpected.

Mind you, a waggish answer to the problem would stop with the first
clause:

(defun valuate-ch (ch)
  (if (alpha-char-p ch)
     0
     nil))

That would seem likely to be a bit more correct than what you
presented, not that "a bit more correct" is a terribly useful
metric...
--
(concatenate 'string "cbbrowne" "@acm.org")
http://www.ntlug.org/~cbbrowne/spiritual.html
Change is inevitable, except from a vending machine.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 66   Newer >
« Back to Discussions « Newer topic     Older topic »