Hi all-
I'm on a mission to learn about computers (beyond just using gui programs), and I've decided to tackle LISP.
Working my way through the examples in the book Land of Lisp (by Dr. Barski, written to cover ANSI Common Lisp), I don't understand when lisp evaluates a number as base 10 versus binary.
For example:
(ash 11 -1) yields 5. Why isn't that yielding 1? (ie: 11 binary = 3 base 10. left shift 11 and you get 2^0 , or 1). Instead it wants to convert 11 to binary first (1011) then left shift it to (101) to get 5. Does that strike anyone else as being weird, or is it just me? It seems that if one of the "operands" (sorry if this is the wrong word) is a binary shift, the other "operand" should be binary too, right?
The remainder of the examples in the book seem to use base-10 numbering without converting to binary, so I'm a little confused.
Is there a "rule of thumb" for when lisp evaluates a number's binary representation first versus using base 10?
Thanks!
D
When does lisp use binary vs. base-10?
Re: When does lisp use binary vs. base-10?
By default, CL always reads and prints numbers as base-10. Internally, it generally doesn't matter how numbers are stored; but for operations like ASH, they are clearly specified as binary.
Re: When does lisp use binary vs. base-10?
Exactly as nuntius said. It's like in other languages. For example in C:
In CL you have standard reader macros:
You can also change a radix used by reader by setting *read-base* variable and by setting *print-base* you change a radix used by printer:
Code: Select all
11 // = 11
0x11 // = 17
011 // = 9
Code: Select all
11 ; = 11
#x11 ; = 17
#o11 ; = 9
#b11 ; = 3
#3r120 ; = 15
Code: Select all
(setf *print-base* 16)
(setf *read-base* 16)
(+ a34b 17fe) ; the result is bb49
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.
-
- Posts: 94
- Joined: Mon Jul 21, 2008 7:26 am
- Location: München, Germany
- Contact:
Re: When does lisp use binary vs. base-10?
Numbers don't have a base. Printed representations of numbers have a base. Thus, base is relevant only when reading and printing numbers. Decimal 5 is the same number as binary 101 (the number five). It's not a conversion. It's the same number.
Lisp always reads numbers according to the current *read-base* setting, which is usually ten. As pointed out by Goheeca, you can easily input numbers in other bases by using the appropriate read-macros.
Lisp always reads numbers according to the current *read-base* setting, which is usually ten. As pointed out by Goheeca, you can easily input numbers in other bases by using the appropriate read-macros.
-
- Posts: 447
- Joined: Sat Jun 28, 2008 7:49 am
- Location: Austin, TX
- Contact:
Re: When does lisp use binary vs. base-10?
This is a good point. Numbers are really abstract concepts. Number representations are concrete and can have many different forms (and have over the centuries), but a representation is not a number.Kompottkin wrote:Numbers don't have a base. Printed representations of numbers have a base. Thus, base is relevant only when reading and printing numbers. Decimal 5 is the same number as binary 101 (the number five). It's not a conversion. It's the same number.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/