For example, given the following function:
(defun deal-card (hand) (push (pop *deck*) hand))
This will, of course, not actually modify the value of hand that was
passed into the function. My question is, if I declare deal-card as
inline, will it then destructively insert the card or would I be better
off making a macro?
Quick side question: what's the difference between DECLAIM, PROCLAIM
and DECLARE? I haven't quite been able to get these on my own. I know
when to use DECLARE and how to use it, but not the other two. The only
difference I've seen is that DECLAIM and PROCLAIM are done at the top
level...
Jeff M.
"Jeff M." <·······@gmail.com> writes:
>
> For example, given the following function:
>
> (defun deal-card (hand) (push (pop *deck*) hand))
>
> This will, of course, not actually modify the value of hand that was
> passed into the function. My question is, if I declare deal-card as
> inline, will it then destructively insert the card
It better not! Inlining of functions is purely an optimization step and
should not change the semantics of the code. Aside from code size and
run time changes, there should be no observable differences in the
resulting code.
> or would I be better off making a macro?
A macro would allow you to do something destructive, but I think you
would be better off either using a return value:
(defun deal-card () (pop *deck*))
...
(push (deal-card) hand)
or else using a mutable structure for the hand:
(defstruct hand (cards nil))
(defun deal-card (hand)
(push (pop *deck*) (hand-cards hand)))
--
Thomas A. Russ, USC/Information Sciences Institute
Up spake Jeff M.:
> Quick side question: what's the difference between DECLAIM, PROCLAIM
> and DECLARE? I haven't quite been able to get these on my own. I know
> when to use DECLARE and how to use it, but not the other two. The only
> difference I've seen is that DECLAIM and PROCLAIM are done at the top
> level...
From Graham's ACL:
(declaim declaration-spec) Macro
Like proclaim, but top-level calls are processed by the compiler,
and declaration-spec is not evaluated.
(declare declaration-spec*)
Not an operator, but resembles one in that an expression whose car
is declare may appear in the beginning of a body of code. Such an
expression makes the declarations described by the declaration-specs
(not evaluated) apply to all the code in the environment in which
declare appears. The following declarations are allowed:
dynamic-extent, ftype, ignorable, ignore, inline, notinline,
optimize, special, type.
(proclaim declaration-spec) Function
Globally makes the declaration described by declaration-spec. The
following declarations are allowed: declaration, ftype, inline,
notinline, optimize, special, type.
--
-trent
I'm a civil servant. It's bloody hard to fire me.
...because you don't fit through the barrel?
The actual phrase on his employee evaluation was `of unmatched calibre'.
Yes, I've got ANSI Common Lisp and have read that, and perhaps I'm
dense, but I'm not seeing the difference (without examples). For
example, why can't I say:
(declare (optimize (speed 3) (safety 0)))
at the top level? Why must this be done with DECLAIM? Likewise, why not
use PROCLAIM to do this? Thanks for the reply, but I was hoping for a
little more :-)
Jeff M.
"Jeff M." <·······@gmail.com> writes:
> Yes, I've got ANSI Common Lisp and have read that, and perhaps I'm
> dense, but I'm not seeing the difference (without examples). For
> example, why can't I say:
>
> (declare (optimize (speed 3) (safety 0)))
>
> at the top level? Why must this be done with DECLAIM? Likewise, why not
> use PROCLAIM to do this? Thanks for the reply, but I was hoping for a
> little more :-)
PROCLAIM is a function so if you put a PROCLAIM as a top-level form in
a file it has no effect until the file is loaded (and the form
evaluated). Which is too late for it to affect the compilation of the
file itself. DECLAIM is a macro that expands (more or less) into a
PROCLAIM wrapped in an EVAL-WHEN so it is evaluated during compilation
and also at load time. You should probably always use DECLAIM.
-Peter
--
Peter Seibel ·····@javamonkey.com
Lisp is the red pill. -- John Fraser, comp.lang.lisp
"Jeff M." <·······@gmail.com> writes:
>> why can't I say:
>>
>> (declare (optimize (speed 3) (safety 0)))
Peter Seibel <·····@javamonkey.com> writes:
> PROCLAIM is a function [..] DECLAIM is a macro [..]
While declare isn't an operator at all, which is why it makes no sense
to "say" it at the top-level. It is merely a symbol that is recognized
by the evaluator/compiler at particiluar positions relative to other
operators such as let, defun, etc.
--
Frode Vatvedt Fjeld
"Jeff M." <·······@gmail.com> writes:
> For example, given the following function:
>
> (defun deal-card (hand) (push (pop *deck*) hand))
>
> This will, of course, not actually modify the value of hand that was
> passed into the function. My question is, if I declare deal-card as
> inline, will it then destructively insert the card or would I be
> better off making a macro?
Inlining will not change semantics such as these at all. (It can only
change things like execution speed and what happens if deal-card is
re-defined.) For example, a call such as
... (deal-card left-hand) ...
with inlining might be thought of as being compiled as
...
(let ((hand left-hand))
(push (pop *deck*) hand))
...
However, it seems to me that your function is better written as
(defun deal-card (hand) (cons (pop *deck*) hand))
..which means you probably would want to call it as
(setf left-hand (deal-card left-hand))
..which I suppose means that you'd be better off doing something like
(defun get-card () (pop *deck*))
..and use it like
(push (get-card) left-hand)
--
Frode Vatvedt Fjeld
"Jeff M." <·······@gmail.com> writes:
> My question is, if I declare deal-card as inline, will it then
> destructively insert the card or would I be better off making a
> macro?
Declaring deal-card as inline will not change the semantics of
parameter passing, so it will not help. A macro would work.
Another approach would be to box the cards in the hand, e.g.
(defun deal-card (hand (push (pop *deck*) (hand-cards hand))))