Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

argument passing: defaults

3 views
Skip to first unread message

Bruno Haible

unread,
Sep 22, 1997, 3:00:00 AM9/22/97
to

Sam Steingold <sshte...@cctrading.com> wrote:
> (defun xx (&optional a) (format t "xx: a: ~a~%" a) (zz a))
>
> But I want zz to use the default argument! I did not give xx any arg,
> and I don't want zz to get any arg either.

You can have this, but it's not beautiful:

(defun xx (&rest args)
(apply (lambda (&optional a)
(format t "xx: a: ~a~%" a)
(apply #'zz args)
)
args
) )

An alternative would be to introduce a special value denoting the default
value, say :default or :unspecific or so:

(defun zz (&optional (a ':default))
(when (eq a ':default) (setq a t))
...
)


Bruno


Barry Margolin

unread,
Sep 22, 1997, 3:00:00 AM9/22/97
to

In article <u67rt1...@WINTERMUTE.eagle>,
SDS <sshte...@cctrading.com> wrote:
](defun zz (&optional (a t)) (format t "zz: a: ~a~%" a))
](defun xx (&optional a) (format t "xx: a: ~a~%" a) (zz a))
](xx)
]xx: a: nil
]zz: a: nil
]nil
]
]Quite reasonable. I understand why I am getting this just fine.
]But I want zz to use the default argument! I did not give xx any arg,

]and I don't want zz to get any arg either.
]Is it possible to deal with this without either resorting to *any*
]redefining of zz (I *cannot* put (setq a (or a t)) in zz) or a kludge in
]xx like (if a (zz a) (zz))?

You can make use of the supplied-p argument that can be used with optional
arguments:

(defun xx (&optional (a nil a-supplied-p))
(format t "~&xx: a: ~A~%" a)
(if a-supplied-p
(zz a)
(zz)))

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.

Kent M Pitman

unread,
Sep 23, 1997, 3:00:00 AM9/23/97
to

In article <u67rt1...@WINTERMUTE.eagle> SDS <sshte...@cctrading.com> writes:

> (defun zz (&optional (a t)) (format t "zz: a: ~a~%" a))
> (defun xx (&optional a) (format t "xx: a: ~a~%" a) (zz a))
> (xx)
> xx: a: nil
> zz: a: nil
> nil
>

> .... But I want zz to use the default argument! ...

(defun xx (&rest args) ;[1]
(format t "xx: a: ~a~%" (first args))
(apply #'zz args))

or

(defun xx (&optional (a nil a-p)) ;[2]
;; Note that variable a-p will be t if a is supplied
;; and nil otherwise; it's called a "supplied-p parameter".


(format t "xx: a: ~a~%" a)

(if a-p (zz a) (zz)))

or

(defun xx (&optional (a t)) ;[3]


(format t "xx: a: ~a~%" a)
(zz a))

NOTES:

If you like [1] but want a to be available, too, you can use:

(defun xx (&rest args) ;[1a]
(destructuring-bind (&optional a) args


(format t "xx: a: ~a~%" a)

(apply #'zz args)))

As for [3], the point is that one can just do the same arg defaulting
in multiple functions. It's sometimes tedious and easy to get out of
alignment if the default changes in one since you have to remember
to update the other functions. But it is possible, and in some
situations it's the preferred approach.

Bernhard Pfahringer

unread,
Sep 23, 1997, 3:00:00 AM9/23/97
to

In article <u67rt1...@WINTERMUTE.eagle>,
SDS <sshte...@cctrading.com> wrote:
>(defun zz (&optional (a t)) (format t "zz: a: ~a~%" a))
>(defun xx (&optional a) (format t "xx: a: ~a~%" a) (zz a))
>(xx)
>xx: a: nil
>zz: a: nil
>nil
>
>xx like (if a (zz a) (zz))?
>
This kludge wont help with (xx nil), but the following will:

(defun xx (&optional (a nil a-supplied?))


(format t "xx: a: ~a~%" a)

(if a-supplied?
(zz a)
(zz)))

(xx)
xx: a: NIL
zz: a: T

(xx nil)
xx: a: NIL
zz: a: NIL
--
--------------------------------------------------------------------------
Bernhard Pfahringer
Austrian Research Institute for http://www.ai.univie.ac.at/~bernhard/
Artificial Intelligence bern...@ai.univie.ac.at

Erik Naggum

unread,
Sep 24, 1997, 3:00:00 AM9/24/97
to

* sshte...@cctrading.com
| (defun zz (&key (ar1 ..) (ar2 ..) (ar3 ..)) ...)
| (defun xx (&key (ar1 nil ar1p) (ar2 nil ar1p) (ar3 nil ar3p))
| (apply #'zz (nconc (if ar1p (list :ar1 ar1)) (if ar2p (list :ar2 ar2))
| (if ar3p (list :ar3 ar3)))))
|
| Of course, this is suitable only is xx is not called too often - because
| of the extra consing. Or maybe not? You have to cons up the list of args
| for zz anyway, right?

this looks like a case for

(defun xx (&rest args &key ...)
(apply #'zz args))

if you don't need access to the actual arguments, you don't even need to
specify keyword arguments in xx. keywords arguments aren't processed at
all if there is no &key in the lambda list.

#\Erik
--
if you think this year is "97", _you_ are not "year 2000 compliant".

see http://www.naggum.no/emacs/ for Emacs-20-related material.

0 new messages