From: Ic2
Subject: a simple question from newbie
Date: 
Message-ID: <l9vM3.508$RK4.985833@news.optonline.net>
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.

From: Robert Monfera
Subject: Re: a simple question from newbie
Date: 
Message-ID: <380293AD.E46A3890@fisec.com>
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.
From: David D. Smith
Subject: Re: a simple question from newbie
Date: 
Message-ID: <dds-1110992316520001@p086.bit-net.com>
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