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
Integer with base preserved!
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 54 - 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
 
Kaz Kylheku  
View profile  
 More options Jan 19 2004, 8:29 pm
Newsgroups: comp.lang.lisp
From: k...@ashi.footprints.net (Kaz Kylheku)
Date: 19 Jan 2004 17:29:13 -0800
Local: Mon, Jan 19 2004 8:29 pm
Subject: Integer with base preserved!
Hi, I don't have a clue here. I understand that Lisp is not a
base-sensitive language; it turns everything into base 10. When I
enter an integer as #xFF, later it prints as 255! Is there a function
like:

   (original-number value)

that, given a number, will retrieve the original hex number?

:) :) :) :)


 
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.
Adam Warner  
View profile  
 More options Jan 19 2004, 10:02 pm
Newsgroups: comp.lang.lisp
From: Adam Warner <use...@consulting.net.nz>
Date: Tue, 20 Jan 2004 16:02:38 +1300
Local: Mon, Jan 19 2004 10:02 pm
Subject: Re: Integer with base preserved!
Hi Kaz Kylheku,

> Hi, I don't have a clue here. I understand that Lisp is not a
> base-sensitive language; it turns everything into base 10. When I
> enter an integer as #xFF, later it prints as 255! Is there a function
> like:

>    (original-number value)

> that, given a number, will retrieve the original hex number?

> :) :) :) :)

[The following code is evil]

(defun |#x| (in sub-char num-arg)
  (declare (ignore sub-char num-arg))
  (let* ((*read-base* 16)
         (integer (read in t nil t))
         (symbol (intern (write-to-string integer))))
    (setf (get symbol :input-type) :hex)
    (set symbol integer)))

(set-dispatch-macro-character #\# #\x #'|#x|)

(defun number-type (value)
  (let ((string-name (write-to-string value)))
    (if (find-symbol string-name)
        (prog1
            (get (intern string-name) :input-type)
          (unintern (intern string-name)))
        :integer)))

(number-type 255)  => :integer
(number-type #xff) => :hex
(number-type 255)  => :integer

Your question while humorous exposes a limitation of the Common Lisp type
system. To be able to operate upon source code at the list level requires
the ability to reconstruct the types of objects to a much greater level of
detail than the type system provides. If there was a way to enforce types
to be kept at say safety 4 then the type system itself could be used for
this purpose, e.g. a new type could be defined as HEX which was a subtype
of INTEGER and TYPEP could be used to check whether an INTEGER was subtype
HEX. But as (a) type information can be thrown away by an implementation
at any level of safety; and (b) custom types are translated to
implementation types, the type system cannot be portably used for this
purpose [this is a challenge: Someone please prove me wrong].

Common Lisp isn't the best language I can imagine. It's simply the best
one available.

Regards,
Adam


 
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 Jan 19 2004, 11:33 pm
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 20 Jan 2004 04:33:24 +0000
Local: Mon, Jan 19 2004 11:33 pm
Subject: Re: Integer with base preserved!
* Adam Warner
| Your question while humorous exposes a limitation of the Common Lisp
| type system.

  Nonsense.

| To be able to operate upon source code at the list level requires the
| ability to reconstruct the types of objects to a much greater level of
| detail than the type system provides.

  Common Lisp is not defined on the character strings that make up the
  source code.  If you want to manipulate the character sequence that
  makes up the source code, you MUST NOT give it to the Common Lisp
  reader to convert it into Common Lisp objects.

  We had this discussion endlessly and without resolution in the SGML
  community a decade ago.  People worried ceaselessly about how they
  would parse SGML documents such that they could re-create the exact
  character sequence that made up the document.  This obsession with the
  source form halted all work on tools to manipulate SGML documents
  intelligently, because it is in fact completely irrelevant what the
  character sequence of the source is as long as multiple forms have
  exactly the same semantics.  Comments in particular caused a lot of
  grief, but you MUST realize that if comments are important to you, you
  MUST NOT give them to the Common Lisp reader or any other parser for
  which they are not only unimportant, but completely irrelevant.

  Common Lisp offers its programmers the tools they need to manipulate
  the source at a level where the character sequences does not matter,
  and this tradition in the Lisp family is quite possibly the single
  most intelligent property of the whole Lisp tradition.  (And I have
  not mentioned Scheme, OK?)

| Common Lisp isn't the best language I can imagine. It's simply the
| best one available.

  Why involve the whole world in your imagination in a negative way like
  this?  We already know that your imagination includes thinking about
  the wasted cons cell of the QUOTE operator, so it is hard not to think
  it is somewhat lacking in working within the world just the way it is,
  but an «imagination» of the impossible is not useful unless you aspire
  to publish your fantasy writings or perhaps special effects software.
  Whatever you imagine, it must be realizable at some level, and you
  have to keep track of which parts of your imagination are going to be
  realizable and which are going to involve the imagination of other
  people.  I maintain that programming /languages/ is not a good arena
  for unrealizable, speculative imagination.

--
Erik Naggum | Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
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.
Greg Menke  
View profile  
 More options Jan 20 2004, 12:16 am
Newsgroups: comp.lang.lisp
From: Greg Menke <gregm-n...@toadmail.com>
Date: 20 Jan 2004 00:16:00 -0500
Subject: Re: Integer with base preserved!

k...@ashi.footprints.net (Kaz Kylheku) writes:
> Hi, I don't have a clue here. I understand that Lisp is not a
> base-sensitive language; it turns everything into base 10. When I
> enter an integer as #xFF, later it prints as 255! Is there a function
> like:

>    (original-number value)

> that, given a number, will retrieve the original hex number?

> :) :) :) :)

Why not use (format nil "~X" value) to get the string representation
of the number's value in hex, which is probably what you're after.

Base 10 is convienent for humans so it makes a reasonable default, but
you are in no way required to keep it set that way.  You might try
experimenting with *read-base* and *print-base*.

Gregm


 
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.
Adam Warner  
View profile  
 More options Jan 20 2004, 2:46 am
Newsgroups: comp.lang.lisp
From: Adam Warner <use...@consulting.net.nz>
Date: Tue, 20 Jan 2004 20:46:45 +1300
Local: Tues, Jan 20 2004 2:46 am
Subject: Re: Integer with base preserved!
Hi Erik Naggum,

> * Adam Warner
> | Your question while humorous exposes a limitation of the Common Lisp
> | type system.

>   Nonsense.

> | To be able to operate upon source code at the list level requires the
> | ability to reconstruct the types of objects to a much greater level of
> | detail than the type system provides.

>   Common Lisp is not defined on the character strings that make up the
>   source code.  If you want to manipulate the character sequence that
>   makes up the source code, you MUST NOT give it to the Common Lisp
>   reader to convert it into Common Lisp objects.

/home/adam/t/feed-the-troll.lisp:
---------------------------------
(defun utility (necessities luxuries dpr-value work-hours leisure-hours)
  "UTILITY is composed of:
1. Ability to satify necessities of life.
2. Amount of luxuries consumed.
3. Value/utils from fulfiling digital processing requirements.
4. Hours available for leisure/non-digital processing requirements."
  #.+optimize+
  (declare (type (double-float 0d0)
                 necessities luxuries dpr-value work-hours leisure-hours))
  (with-violation-support
      (* (if (>= necessities 1d0)
             1d0
             (sqrt (- 1d0 (square (- 1d0 necessities)))))
         (collect-violations (effectiveness work-hours leisure-hours))
         (+ (diminishing-utility #.+base-utils-from-luxuries+ luxuries)
            dpr-value ;;dpr-fulfilment->dpr-value already incorporates diminishing utility
            (diminishing-utility #.+base-utils-from-leisure-hours+ leisure-hours)))))
---------------------------------

(in-package capture-source)
(car (load-source "/home/adam/t/feed-the-troll.lisp")) =>

(defun utility (necessities luxuries dpr-value work-hours leisure-hours)
  "UTILITY is composed of:
1. Ability to satify necessities of life.
2. Amount of luxuries consumed.
3. Value/utils from fulfiling digital processing requirements.
4. Hours available for leisure/non-digital processing requirements."
  (|#.| . +optimize+)
  (declare
   (type (double-float 0.0) necessities luxuries dpr-value work-hours
    leisure-hours))
  (with-violation-support
   (* (if (>= necessities 1.0) 1.0 (sqrt (- 1.0 (square (- 1.0 necessities)))))
      (collect-violations (effectiveness work-hours leisure-hours))
      (+ (diminishing-utility (|#.| . +base-utils-from-luxuries+) luxuries)
         dpr-value
         (|;|
          . ";;dpr-fulfilment->dpr-value already incorporates diminishing utility")
         (diminishing-utility (|#.| . +base-utils-from-leisure-hours+)
          leisure-hours)))))

All done by the Lisp reader. Notice the unevaluated #. and the capture of
comments. This could be translated back to source text. Partly a proof of
concept. I can even handle the (non-)interning of symbols with a
(non-keyword) package prefix. The only place comments cannot appear is
within a dotted list, but I could handle this as well by further rewrites
of the list reader. The code is entirely portable and coexists with
standard reader definitions.

(read-list (make-string-input-stream "unknown-package:symbol)") #\( :custom t)
=> ((|:| . "unknown-package:symbol"))

(read-list (make-string-input-stream "unknown-package:symbol)") #\( :custom nil)
=> Reader error at 22 on #<String-Input Stream>:
Package "UNKNOWN-PACKAGE" not found.
   [Condition of type lisp::reader-package-error]

By using my methods of legitimate inquiry (e.g. asking questions, like
why QUOTE wastes a cons cell) as a weapon to attack me just reinforces the
hypocrisy of your self-presentation as a man of reason.

One explanation that forms like the above cannot in general be evaluated
as Lisp code is the insistence of Common Lisp to turn forms that return
zero values into nil. I will continue to use my imagination to consider
changes to Common Lisp and I will continue to work upon ideas you dismiss
as unrealisable.

It's unusual for me to choose to reply to you Erik. I don't make the
mistake often.

Regards,
Adam


 
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.
Tim Bradshaw  
View profile  
 More options Jan 20 2004, 5:35 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@cley.com>
Date: Tue, 20 Jan 2004 09:38:00 +0000
Subject: Re: Integer with base preserved!

* Kaz Kylheku wrote:
> Hi, I don't have a clue here. I understand that Lisp is not a
> base-sensitive language; it turns everything into base 10. When I
> enter an integer as #xFF, later it prints as 255! Is there a function
> like:

I'm afraid you are confused.  CL is really a MACLISP derivative, and
as such its internal representation is, of course, octal.

 
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.
Tim Bradshaw  
View profile  
 More options Jan 20 2004, 5:35 am
Newsgroups: comp.lang.lisp
From: Tim Bradshaw <t...@cley.com>
Date: Tue, 20 Jan 2004 09:42:56 +0000
Local: Tues, Jan 20 2004 4:42 am
Subject: Re: Integer with base preserved!

* Erik Naggum wrote:
>   Comments in particular caused a lot of
>   grief, but you MUST realize that if comments are important to you, you
>   MUST NOT give them to the Common Lisp reader or any other parser for
>   which they are not only unimportant, but completely irrelevant.

If you insist that you *can* give them to the reader, then you end up
with InterLISP.  I remember that the Medley handling of things like #+
and #- was interesting in the sort of really bad way you'd expect.

--tim


 
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.
Kaz Kylheku  
View profile  
 More options Jan 20 2004, 12:31 pm
Newsgroups: comp.lang.lisp
From: k...@ashi.footprints.net (Kaz Kylheku)
Date: 20 Jan 2004 09:31:14 -0800
Local: Tues, Jan 20 2004 12:31 pm
Subject: Re: Integer with base preserved!

Tim Bradshaw <t...@cley.com> wrote in message <news:ey3y8s32c5j.fsf@cley.com>...
> * Kaz Kylheku wrote:
> > Hi, I don't have a clue here. I understand that Lisp is not a
> > base-sensitive language; it turns everything into base 10. When I
> > enter an integer as #xFF, later it prints as 255! Is there a function
> > like:

> I'm afraid you are confused.  CL is really a MACLISP derivative, and
> as such its internal representation is, of course, octal.

No way dude, the Mac was based on the 68000 chip, which uses hex (with
a $ prefix, as in move.l #$FF, (A0) and all dat).

:)


 
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.
Joe Marshall  
View profile  
 More options Jan 20 2004, 3:04 pm
Newsgroups: comp.lang.lisp
From: Joe Marshall <j...@ccs.neu.edu>
Date: Tue, 20 Jan 2004 15:03:12 -0500
Local: Tues, Jan 20 2004 3:03 pm
Subject: Re: Integer with base preserved!

Adam Warner <use...@consulting.net.nz> writes:
> (number-type 255)  => :integer
> (number-type #xff) => :hex
> (number-type 255)  => :integer

> Your question while humorous exposes a limitation of the Common Lisp type
> system.  

There is nothing wrong with the type system.  That it does not conform
to your imagination is not a limitation.

> To be able to operate upon source code at the list level requires
> the ability to reconstruct the types of objects to a much greater level of
> detail than the type system provides.  If there was a way to enforce types
> to be kept at say safety 4 then the type system itself could be used for
> this purpose, e.g. a new type could be defined as HEX which was a subtype
> of INTEGER and TYPEP could be used to check whether an INTEGER was subtype
> HEX.  But as (a) type information can be thrown away by an implementation
> at any level of safety;

Type information is not thrown away.  The fact that the garbage
collector works is evidence of this.

> and (b) custom types are translated to implementation types, the
> type system cannot be portably used for this purpose [this is a
> challenge: Someone please prove me wrong].

(defstruct hex
  value)

(vectorp (make-hex :value 33)) => NIL

However, most implementations of defstruct allocate a linear region of
memory.  Some even allocate a literal vector before re-tagging it to
indicate a struct.


 
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.
Pascal Bourguignon  
View profile  
 More options Jan 20 2004, 5:23 pm
Newsgroups: comp.lang.lisp
From: Pascal Bourguignon <s...@thalassa.informatimago.com>
Date: 20 Jan 2004 23:22:19 +0100
Local: Tues, Jan 20 2004 5:22 pm
Subject: Re: Integer with base preserved!

k...@ashi.footprints.net (Kaz Kylheku) writes:
> Tim Bradshaw <t...@cley.com> wrote in message <news:ey3y8s32c5j.fsf@cley.com>...
> > * Kaz Kylheku wrote:
> > > Hi, I don't have a clue here. I understand that Lisp is not a
> > > base-sensitive language; it turns everything into base 10. When I
> > > enter an integer as #xFF, later it prints as 255! Is there a function
> > > like:

> > I'm afraid you are confused.  CL is really a MACLISP derivative, and
> > as such its internal representation is, of course, octal.

> No way dude, the Mac was based on the 68000 chip, which uses hex (with
> a $ prefix, as in move.l #$FF, (A0) and all dat).

> :)

All of you are plain wrong.

It's well known  that numbers are actually represented  by little imps
inside the computers.   There are three kind of  imps: the mosig imps,
the lesig imps  and the midsig imps, (each coming  in three sexes: the
littlends,  the bigends, and  the mixends,  but strangely  enough, you
generally find  only imps of one  sex in a given  computer).  Some say
that  there are  also one  kind of  unsig imps,  but  they're probably
normal imps  hidding out of the  light. So all these  little imps hold
their  hands in  round, in  groups of  32, or  more often  nowadays in
groups of 64, but we still find  some small groups of 16 or 8, or even
of 9 and 36.  And depending on whether they've been struck on the head
by the BSD daemon,  they may have a bump on the  head and be crying or
not.   The more  they cry,  the higher  the value  of the  number they
represent.  (That ought to be the  same with politicians but it is not
unfortunately).

--
__Pascal_Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/


 
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.
Joseph Oswald  
View profile  
 More options Jan 20 2004, 7:32 pm
Newsgroups: comp.lang.lisp
From: josephoswal...@hotmail.com (Joseph Oswald)
Date: 20 Jan 2004 16:32:05 -0800
Local: Tues, Jan 20 2004 7:32 pm
Subject: Re: Integer with base preserved!

k...@ashi.footprints.net (Kaz Kylheku) wrote in message <news:cf333042.0401191729.207b5eac@posting.google.com>...
> Hi, I don't have a clue here. I understand that Lisp is not a
> base-sensitive language; it turns everything into base 10. When I
> enter an integer as #xFF, later it prints as 255! Is there a function
> like:

>    (original-number value)

> that, given a number, will retrieve the original hex number?

Kaz--

  I'm not sure what the smiley faces were about, but Lisp does not
"turn" a number into anything but that number. Numeric bases are
properties of of the representation of a number, not a property of a
number.

  Lisp reads in a series of characters (a representation), interprets
them according to the value of *read-base*, in order to produce an
integer number (actually, a representation of that number as a pattern
of bits in memory, but that's as close as a computer can get to a
number).

  Because humans have a difficult time seeing the memory bits inside
the machine, it is conventional to print out things in a
representation that a human can parse. In the case of Common Lisp, it
prints out numerals that denote the number in the base determined by
the value of *print-base*.

  #xFF is a representation of the same number represented by
#b11111111, which is the same number that is represented by 255 with
*read-base* bound to the number represented by #xA.

  Confusing the representation of a thing with the thing itself is one
of the commonest errors in thought. Common Lisp generally avoids these
errors pretty well.

  Consider binding *print-base* to get the effect you are looking for.


 
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.
Pascal Bourguignon  
View profile  
 More options Jan 20 2004, 11:07 pm
Newsgroups: comp.lang.lisp
From: Pascal Bourguignon <s...@thalassa.informatimago.com>
Date: 21 Jan 2004 05:05:50 +0100
Local: Tues, Jan 20 2004 11:05 pm
Subject: Re: Integer with base preserved!

Or, said otherwise,

http://www.google.com/groups?selm=87y8vuyrgi.fsf%40thalassa.informati...

what in what base should these numbers be considered to be?

[53]> (+s "nine" "five")
"fourteen"
[54]> (-s "eleven" "three")
"eight"
[55]> (*s
  "eight hundred and seventy-three thousand, five hundred and seventy-one"
  "seven hundred and sixty-six")
 "six hundred and sixty-nine million, one hundred and fifty-five thousand, three hundred and eighty-six"

(and check  the implementation, it really uses  COMMON-LISP numbers to
compute the values).

--
__Pascal_Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/


 
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.
Adam Warner  
View profile  
 More options Jan 20 2004, 11:51 pm
Newsgroups: comp.lang.lisp
From: Adam Warner <use...@consulting.net.nz>
Date: Wed, 21 Jan 2004 17:51:51 +1300
Local: Tues, Jan 20 2004 11:51 pm
Subject: Re: Integer with base preserved!
Hi Joe Marshall,

#S(hex :value 33) doesn't evaluate to 33. Using a defined type does:

(deftype hex () 'integer)
(the hex 33) => 33

But the "presentation" type information is lost (the custom type is
translated to the implementation type).

If we replace 33 with the structure #S(hex :value 33) then a program no
longer evaluates the same, i.e. implementations have no understanding of
how to process (numeric) objects that are wrapped up with presentation
data.

I'm defining a presentation type as a piece of information attached to an
object that has no effect upon standard program evaluation and semantics.

This could be implemented using structures. Many of the Common Lisp
functions would have to be rewritten to discard the presentation
information.

Here's a partial implementation of Kaz's question:

(defpackage kaz
  (:use)
  (:import-from #:cl #:t #:nil #:&rest #:&optional))

(in-package kaz)

(cl:defstruct num
  hex value)

(cl:defun |read-!x| (in sub-char num-arg)
  (cl:declare (cl:ignore sub-char num-arg))
  (cl:let* ((cl:*read-base* 16)
            (value (cl:read in t nil t)))
    (make-num :hex t :value value)))

(cl:make-dispatch-macro-character #\!)
(cl:set-dispatch-macro-character #\! #\x #'|read-!x|)

(cl:defun orig-num (value)
  (cl:cond ((num-p value)
            (cl:when (num-hex value)
              (cl:format nil "#x~X" (num-value value))))
           (t (cl:format nil "~S" value))))

(cl:defun + (&rest args)
  (cl:if (cl:member-if #'num-p args)
         (cl:apply #'cl:+ (cl:mapcar #'(cl:lambda (obj)
                                         (cl:if (num-p obj)
                                                (num-value obj)
                                                obj)) args))
         (cl:apply #'cl:+ args)))

(cl:defun print (obj &optional out-stream)
  (cl:if (num-p obj)
         (cl:print (num-value obj) out-stream)
         (cl:print obj out-stream)))

! is used in place of # as the dispatching macro character to avoid
upsetting the standard reader. I would also store the actual text
composing the number rather than just a flag for hexadecimal in a better
implementation (e.g. #x00ff contains presentational information that
cannot be captured by converting 255 back to hexadecimal).

Thus I can now write:

(in-package kaz)
(+ !xff 1) => 256
(print !xff) => prints and returns 255.
(orig-num 255) => "255"
(orig-num !xff) => "#xFF"
(orig-num (+ !xff 1)) => "256"

[The last form demonstrates that the presentation information is discarded
once an object is modified or created]

Supporting ORIG-NUM has severe performance implications. The least impact
would probably come from a few type bits reserved for presentation
information, and this would probably only be viable upon 64-bit platforms.
Another method would be all objects attached to a presentation "pointer"
(that may also provide standard type information. Under particular
circumstances (low safety, good type declarations, non-use of TYPEP) these
pointers could be eliminated all together. Highly declared code could even
avoid type bit manipulation for integer arithmetic).

If we limit Kaz's requirements to reproducing the presentation information
at the source code level then the functionality can implemented with no
compiled-code performance impact:

(defstruct num
  hex value)

(defun |read-!x| (in sub-char num-arg)
  (declare (ignore sub-char num-arg))
  (let* ((*read-base* 16)
            (value (read in t nil t)))
    `(a-number ,(make-num :hex t :value value))))

(make-dispatch-macro-character #\!)
(set-dispatch-macro-character #\! #\x #'|read-!x|)

(defmacro a-number (num)
  (num-value num))

Now !xff evaluates and compiles to 255. But when quoted to inhibit the
evaluation it's (a-number #S(num :hex t :value 255)). So the above line of
source code (+ !xff 1) would be read as (+ (a-number #S(num :hex t :value
255)) 1) and when walking the list form of the code one would know when a
hexadecimal number is supposed to be printed instead of a decimal one.

But a (quote #xff) does not equal (quote !xff) the !x dispatching macro
can not replace #x.

Regards,
Adam


 
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.
Adam Warner  
View profile  
 More options Jan 20 2004, 11:59 pm
Newsgroups: comp.lang.lisp
From: Adam Warner <use...@consulting.net.nz>
Date: Wed, 21 Jan 2004 17:59:42 +1300
Local: Tues, Jan 20 2004 11:59 pm
Subject: Re: Integer with base preserved!
Hi Joe Marshall,

#S(hex :value 33) doesn't evaluate to 33. Using a defined type does:

(deftype hex () 'integer)
(the hex 33) => 33

But the "presentation" type information is lost (the custom type is
translated to the implementation type).

If we replace 33 with the structure #S(hex :value 33) then a program no
longer evaluates the same, i.e. implementations have no understanding of
how to process (numeric) objects that are wrapped up with presentation
data.

I'm defining a presentation type as a piece of information attached to an
object that has no effect upon standard program evaluation and semantics.

This could be implemented using structures. Many of the Common Lisp
functions would have to be rewritten to discard the presentation
information.

Here's a partial implementation of Kaz's question:

(defpackage kaz
  (:use)
  (:import-from #:cl #:t #:nil #:&rest #:&optional))

(in-package kaz)

(cl:defstruct num
  hex value)

(cl:defun |read-!x| (in sub-char num-arg)
  (cl:declare (cl:ignore sub-char num-arg))
  (cl:let* ((cl:*read-base* 16)
            (value (cl:read in t nil t)))
    (make-num :hex t :value value)))

(cl:make-dispatch-macro-character #\!)
(cl:set-dispatch-macro-character #\! #\x #'|read-!x|)

(cl:defun orig-num (value)
  (cl:cond ((num-p value)
            (cl:when (num-hex value)
              (cl:format nil "#x~X" (num-value value))))
           (t (cl:format nil "~S" value))))

(cl:defun + (&rest args)
  (cl:if (cl:member-if #'num-p args)
         (cl:apply #'cl:+ (cl:mapcar #'(cl:lambda (obj)
                                         (cl:if (num-p obj)
                                                (num-value obj)
                                                obj)) args))
         (cl:apply #'cl:+ args)))

(cl:defun print (obj &optional out-stream)
  (cl:if (num-p obj)
         (cl:print (num-value obj) out-stream)
         (cl:print obj out-stream)))

! is used in place of # as the dispatching macro character to avoid
upsetting the standard reader. I would also store the actual text
composing the number rather than just a flag for hexadecimal in a better
implementation (e.g. #x00ff contains presentational information that
cannot be captured by converting 255 back to hexadecimal).

Thus I can now write:

(in-package kaz)
(+ !xff 1) => 256
(print !xff) => prints and returns 255.
(orig-num 255) => "255"
(orig-num !xff) => "#xFF"
(orig-num (+ !xff 1)) => "256"

[The last form illustrates that the presentation information may (and
should) be discarded within numeric computations]

Supporting ORIG-NUM has severe performance implications. The least impact
would probably come from a few type bits reserved for presentation
information, and this would probably only be viable upon 64-bit platforms.
Another method would be all objects attached to a presentation "pointer"
(that may also provide standard type information. Under particular
circumstances (low safety, good type declarations, non-use of TYPEP) these
pointers could be eliminated all together. Highly declared code could even
avoid type bit manipulation for integer arithmetic).

If we limit Kaz's requirements to reproducing the presentation information
at the source code level then the functionality can implemented with no
compiled-code performance impact:

(defstruct num
  hex value)

(defun |read-!x| (in sub-char num-arg)
  (declare (ignore sub-char num-arg))
  (let* ((*read-base* 16)
            (value (read in t nil t)))
    `(a-number ,(make-num :hex t :value value))))

(make-dispatch-macro-character #\!)
(set-dispatch-macro-character #\! #\x #'|read-!x|)

(defmacro a-number (num)
  (num-value num))

Now !xff evaluates and compiles to 255. But when quoted to inhibit the
evaluation it's (a-number #S(num :hex t :value 255)). So the above line of
source code (+ !xff 1) would be read as (+ (a-number #S(num :hex t :value
255)) 1) and when walking the list form of the code one would know when a
hexadecimal number is supposed to be printed instead of a decimal one.

But as (quote #xff) does not equal (quote !xff) the !x dispatching macro
cannot replace #x.

Regards,
Adam


 
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 Jan 21 2004, 1:09 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 21 Jan 2004 06:09:02 +0000
Local: Wed, Jan 21 2004 1:09 am
Subject: Re: Integer with base preserved!
* Adam Warner
| By using my methods of legitimate inquiry (e.g. asking questions, like
| why QUOTE wastes a cons cell) as a weapon to attack me just reinforces
| the hypocrisy of your self-presentation as a man of reason.

  You seem to hurt because of something I said.  Had you responded less
  aggressively, I might have considered your pain.  Now I do not.

| I will continue to use my imagination to consider changes to Common
| Lisp and I will continue to work upon ideas you dismiss as
| unrealisable.

  It is not I who determine the realizability of your ideas.  If you
  fight with insufficient understanding, you will not accomplish much.
  This ought to have concerned you if the effectiveness of your ideas is
  relevant to you.  Considering your hostile response to my criticism
  and suggestions for more effective ideas and means of realizing them,
  it is unlikely that many people will tell you when you do something
  stupid, and thus you are doomed to learn only from your own mistakes.
  This is not a position anyone should be in unless they have worked
  very hard to deserve it.

| It's unusual for me to choose to reply to you Erik.  I don't make the
| mistake often.

  Once a year seems like a good first approximation to your reply rate.

--
Erik Naggum | Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
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.
Joe Marshall  
View profile  
 More options Jan 21 2004, 10:10 am
Newsgroups: comp.lang.lisp
From: Joe Marshall <j...@ccs.neu.edu>
Date: Wed, 21 Jan 2004 10:10:25 -0500
Local: Wed, Jan 21 2004 10:10 am
Subject: Re: Integer with base preserved!

Adam Warner <use...@consulting.net.nz> writes:
> #S(hex :value 33) doesn't evaluate to 33.

Of course not!  You wanted a HEX type that was distinguishable from CL
integers.

> If we replace 33 with the structure #S(hex :value 33) then a program no
> longer evaluates the same, i.e. implementations have no understanding of
> how to process (numeric) objects that are wrapped up with presentation
> data.

Exactly.  The HEX type is different from the integer type, so you will
have to define what it means to process it numerically.  For instance,
what does it mean to add two HEXs together?  What about if the result
is larger than 255?  What does it mean to divide them?  What about
adding an integer to a HEX?  Can you add OCTals to HEXs?  What is the
result type?

> I'm defining a presentation type as a piece of information attached to an
> object that has no effect upon standard program evaluation and semantics.

That is not usually what is meant by a type.

 
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.
Joe Marshall  
View profile  
 More options Jan 21 2004, 10:16 am
Newsgroups: comp.lang.lisp
From: Joe Marshall <j...@ccs.neu.edu>
Date: Wed, 21 Jan 2004 10:16:31 -0500
Local: Wed, Jan 21 2004 10:16 am
Subject: Re: Integer with base preserved!

From HAKMEM, ITEM 154 (Gosper):

The myth that any given programming language is machine independent is
easily exploded by computing the sum of powers of 2.

 - If the result loops with period = 1 with sign +, you are on a
   sign-magnitude machine.  

 - If the result loops with period = 1 at -1, you are on a
   twos-complement machine.  

 - If the result loops with period > 1, including the beginning, you
   are on a ones-complement machine.  

 - If the result loops with period > 1, not including the beginning,
   your machine isn't binary -- the pattern should tell you the base.

 - If you run out of memory, you are on a string or Bignum system.

 - If arithmetic overflow is a fatal error, some fascist pig with a
   read-only mind is trying to enforce machine independence. But the
   very ability to trap overflow is machine dependent.  

By this strategy, consider the universe, or, more precisely, algebra:
let X = the sum of many powers of two = ...111111
now add X to itself; X + X = ...111110
thus, 2X = X - 1 so X = -1

therefore algebra is run on a machine (the universe) which is
twos-complement.


 
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 Jan 21 2004, 11:00 am
Newsgroups: comp.lang.lisp
From: Marco Antoniotti <marc...@cs.nyu.edu>
Date: Wed, 21 Jan 2004 11:00:29 -0500
Local: Wed, Jan 21 2004 11:00 am
Subject: Re: Integer with base preserved!

Kaz Kylheku wrote:
> Hi, I don't have a clue here. I understand that Lisp is not a
> base-sensitive language; it turns everything into base 10. When I
> enter an integer as #xFF, later it prints as 255! Is there a function
> like:

>    (original-number value)

> that, given a number, will retrieve the original hex number?

> :) :) :) :)

Is this a trick question, whose answer is "RTFM *PRINT-BASE*"? :)

Cheers
--
Marco


 
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.
Timothy Moore  
View profile  
 More options Jan 21 2004, 4:13 pm
Newsgroups: comp.lang.lisp
From: Timothy Moore <mo...@trousse.labri.fr>
Date: 21 Jan 2004 22:21:07 +0100
Local: Wed, Jan 21 2004 4:21 pm
Subject: Re: Integer with base preserved!

Adam Warner <use...@consulting.net.nz> writes:

> If we replace 33 with the structure #S(hex :value 33) then a program no
> longer evaluates the same, i.e. implementations have no understanding of
> how to process (numeric) objects that are wrapped up with presentation
> data.

> I'm defining a presentation type as a piece of information attached to an
> object that has no effect upon standard program evaluation and semantics.

> This could be implemented using structures. Many of the Common Lisp
> functions would have to be rewritten to discard the presentation
> information.

If you want presentation types, you know where to find them: CLIM. The
presentation type information is explicit in the program but implicit
on the display.

Tim


 
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.
Adam Warner  
View profile  
 More options Jan 21 2004, 6:43 pm
Newsgroups: comp.lang.lisp
From: Adam Warner <use...@consulting.net.nz>
Date: Thu, 22 Jan 2004 12:43:19 +1300
Local: Wed, Jan 21 2004 6:43 pm
Subject: Re: Integer with base preserved!
Hi Timothy Moore,

> If you want presentation types, you know where to find them: CLIM. The
> presentation type information is explicit in the program but implicit on
> the display.

Thanks for the tip Timothy. I haven't used CLIM and I wasn't consciously
(mis)appropriating the term.

Regards,
Adam


 
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 A. Russ  
View profile  
 More options Jan 22 2004, 5:40 pm
Newsgroups: comp.lang.lisp
From: t...@sevak.isi.edu (Thomas A. Russ)
Date: 22 Jan 2004 13:23:53 -0800
Local: Thurs, Jan 22 2004 4:23 pm
Subject: Re: Integer with base preserved!

But MACLISP was based on Project MAC at MIT, not on the Macintosh
computer.  And it really was base 8 at heart.  That is why you often saw
integers with trailing decimal points.  A trailing decimal point (with
no other digits behind it) indicated a base-10 integer constant rather
than a base-8 integer constant.

That meant, for example, that

  (= 10 10.)  => NIL
  (= 10 8.)   => T

:D

--
Thomas A. Russ,  USC/Information Sciences Institute


 
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.
Raffael Cavallaro  
View profile  
 More options Jan 22 2004, 6:28 pm
Newsgroups: comp.lang.lisp
From: Raffael Cavallaro <raffaelcavall...@junk.mail.me.not.mac.com>
Date: Thu, 22 Jan 2004 23:28:56 GMT
Local: Thurs, Jan 22 2004 6:28 pm
Subject: Re: Integer with base preserved!
In article <87d69eq735....@thalassa.informatimago.com>,
 Pascal Bourguignon <s...@thalassa.informatimago.com> wrote:

>  "six hundred and sixty-nine million, one hundred and fifty-five thousand,
>  three hundred and eighty-six"

FWIW, the above output is wrong - "and" should only be used between the
whole number and fractional parts, e.g.

six hundred sixty-nine million, one hundred fifty-five thousand, three
hundred eighty-six and three tenths


 
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.
Gareth McCaughan  
View profile  
 More options Jan 22 2004, 8:19 pm
Newsgroups: comp.lang.lisp
From: Gareth McCaughan <gareth.mccaug...@pobox.com>
Date: 23 Jan 2004 01:05:25 +0000
Local: Thurs, Jan 22 2004 8:05 pm
Subject: Re: Integer with base preserved!

Raffael Cavallaro <raffaelcavall...@junk.mail.me.not.mac.com> writes:
> In article <87d69eq735....@thalassa.informatimago.com>,
>  Pascal Bourguignon <s...@thalassa.informatimago.com> wrote:

>>  "six hundred and sixty-nine million, one hundred and fifty-five thousand,
>>  three hundred and eighty-six"

> FWIW, the above output is wrong - "and" should only be used between the
> whole number and fractional parts, e.g.

> six hundred sixty-nine million, one hundred fifty-five thousand, three
> hundred eighty-six and three tenths

That's true in US English. It's not true in British English.
In the absence of a clear indication of which variety of English
Pascal was intending to write, it's not reasonable to dismiss
what he wrote as "wrong".

--
Gareth McCaughan
.sig under construc


 
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.
Pascal Bourguignon  
View profile  
 More options Jan 23 2004, 2:24 am
Newsgroups: comp.lang.lisp
From: Pascal Bourguignon <s...@thalassa.informatimago.com>
Date: 23 Jan 2004 08:22:54 +0100
Local: Fri, Jan 23 2004 2:22 am
Subject: Re: Integer with base preserved!

Gareth McCaughan <gareth.mccaug...@pobox.com> writes:
> Raffael Cavallaro <raffaelcavall...@junk.mail.me.not.mac.com> writes:

> > In article <87d69eq735....@thalassa.informatimago.com>,
> >  Pascal Bourguignon <s...@thalassa.informatimago.com> wrote:

> >>  "six hundred and sixty-nine million, one hundred and fifty-five thousand,
> >>  three hundred and eighty-six"

> > FWIW, the above output is wrong - "and" should only be used between the
> > whole number and fractional parts, e.g.

It's in the same base as the input, though!

> > six hundred sixty-nine million, one hundred fifty-five thousand, three
> > hundred eighty-six and three tenths

> That's true in US English. It's not true in British English.
> In the absence of a clear indication of which variety of English
> Pascal was intending to write, it's not reasonable to dismiss
> what he wrote as "wrong".

I don't know, what are the  difference in English and US numbers?  But
it does not  matter, at least lisp ~R is consistent  and it parsed the
numbers and formated the output in the same language.

--
__Pascal_Bourguignon__                     http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he doesn't
want merely because you think it would be good for him.--Robert Heinlein
http://www.theadvocates.org/


 
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 Jan 23 2004, 2:54 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <e...@naggum.no>
Date: 23 Jan 2004 07:54:31 +0000
Local: Fri, Jan 23 2004 2:54 am
Subject: Re: Integer with base preserved!
* Pascal Bourguignon
| I don't know, what are the  difference in English and US numbers?

  The English-speaking part of the world have enormous problem counting
  any higher than 20, which explains their Fred Flintstone Units and
  their irrational resistance to units that tends to yield measurements
  with values having more than 1 significant digits and their hostility
  towards the French for having invented smarter units, but strangely,
  they adopted the old French way to count large numbers, using the
  number of thousand groups: A million is a thousand thousand, a billion
  is a thousand million, a trillion is a thousand billion, etc, while
  the French changed their ways in 1948 to count the number of millions,
  so a million is a thousand thousand, a billion is a million million,
  and a trillion is a million billion.  You may note that new trillion
  has three million groups, and that this pattern is a lot more sensible
  than the the old trillion which has /four/ thousand groups.

  So when the U.S. federal deficit is two trillion dollars, is appears
  to Europeans to be a million times larger than it really is, which is
  probably the only good thing you could say about it.

--
Erik Naggum | Oslo, Norway

Act from reason, and failure makes you rethink and study harder.
Act from faith, and failure makes you blame someone and push harder.


 
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 54   Newer >
« Back to Discussions « Newer topic     Older topic »