I am new to lisp programming, but I know c/c++.
I would like to know, if it is possible to do typecasting or type conversion
in lisp as in C/C++.
I have read that comman lisp variables are untype, but I just can't grasp it
because what
if you need to change from float to integer?
Any help would be appreciated.
In Common Lisp, values themselves are typed, rather than variables.
Could you explain why exactly you are concerned about type conversions?
You may bind the value 1 to apples, then 3.14. Do a few example
calculations yourself.
> (typep apples 'integer)
T
> (typep apples 'float)
NIL
> (setf apples 1)
1
> (typep apples 'integer)
T
> (typep apples 'float)
NIL
> (setf apples pi)
3.141592653589793
> (typep apples 'integer)
NIL
> (typep apples 'float)
T
> (setf pears (* 3 apples)) ; note that 3 is integer
9.42477796076938
You can actually change the type, but chances are you do not need it
now:
> (coerce 1 'float)
1.0
Robert
Ic2 wrote:
>
> I am new to lisp programming, but I know c/c++.
> I would like to know, if it is possible to do typecasting or type conversion
> in lisp as in C/C++.
> I have read that comman lisp variables are untype, but I just can't grasp it
> because what
> if you need to change from float to integer?
>
> Any help would be appreciated.
In article <····················@news.optonline.net>, "Ic2"
<········@excite.com> wrote:
> I am new to lisp programming, but I know c/c++.
> I would like to know, if it is possible to do typecasting or type conversion
> in lisp as in C/C++.
> I have read that comman lisp variables are untype, but I just can't grasp it
> because what
> if you need to change from float to integer?
>
> Any help would be appreciated.
In LISP it is as if all variables are void* and indirection is automatic.
Additionally any objects type can be determined using TYPE-OF, or the type
predicates.
If you need to change a float to an integer there are four (actually more)
functions that will do that.
FLOOR Greatest Integer -- Round towards Negative Infinity
CEILING Least Inhteger -- Round towards Positive Infinity
ROUND -- Round towards nearest or even integer
TRUNCATE -- Round towards 0
(FLOOR 3.14) => 3 (FLOOR -3.94) => -4
(CEILING 3.14) => 4 (CEILING -3.94) => -3
(ROUND 3.14) => 3 (ROUND 3.94) => 4
(TRUNCATE 3.14) => 3 (TRUNCATE -3.14) => -3
d
From: Christopher R. Barry
Subject: Re: a simple question from newbie
Date:
Message-ID: <87u2nxuwjr.fsf@2xtreme.net>
"Ic2" <········@excite.com> writes:
> I am new to lisp programming, but I know c/c++.
> I would like to know, if it is possible to do typecasting or type conversion
> in lisp as in C/C++.
> I have read that comman lisp variables are untype, but I just can't grasp it
> because what
> if you need to change from float to integer?
>
> Any help would be appreciated.
Use COERCE for your basic type conversion needs. If you want to
convert floats to integers, there are a number of functions like
ROUND, TRUNCATE, CEILING, etc. See:
http://www.harlequin.com/education/books/HyperSpec/
Christopher