Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## clisp-list

 [clisp-list] beginner questions From: Mark Volkmann - 2011-01-18 02:26:38 I'm trying to learn about optional arguments. In the function below, how could I return the sum of a, b, c & d? It's not obvious to me since c and/or d may be nil. It seems I need to learn the best way to test for nil values. (defun opt-args (a b &optional c d) ...) -- R. Mark Volkmann Object Computing, Inc.
 Re: [clisp-list] beginner questions From: Dan Becker - 2011-01-18 03:34:01 This is more of a comp.lang.lisp thing, but since you are already here ... You can set a default value for the optional arguments. So if the idea is "return the sum of all provided arguments", then .. [1]> (defun opt-args (a b &optional (c 0) (d 0)) (+ a b c d)) OPT-ARGS [2]> (opt-args 10 20) 30 [3]> (opt-args 10 20 30) 60 [4]> (opt-args 10 20 30 100) 160 You can check for nil values using the NULL predicate: [5]> (null nil) T But in this particular case, if you wanted to avoid default arguments then I think the following would be best: [6]> (defun opt-args (a b &optional c d) (cond ((and c d) (+ a b c d)) (c (+ a b c)) (t (+ a b)))) OPT-ARGS [7]> (opt-args 10 20) 30 [8]> (opt-args 10 20 30) 60 [9]> (opt-args 10 20 30 100) 160 Remember that in common lisp, anything that isn't NIL evaluates as true! On Mon, Jan 17, 2011 at 7:26 PM, Mark Volkmann wrote: > I'm trying to learn about optional arguments. > In the function below, how could I return the sum of a, b, c & d? > It's not obvious to me since c and/or d may be nil. > It seems I need to learn the best way to test for nil values. > > (defun opt-args (a b &optional c d) >  ...) > > -- > R. Mark Volkmann > Object Computing, Inc. > > ------------------------------------------------------------------------------ > Protect Your Site and Customers from Malware Attacks > Learn about various malware tactics and how to avoid them. Understand > malware threats, the impact they can have on your business, and how you > can protect your company and customers by using code signing. > http://p.sf.net/sfu/oracle-sfdevnl > _______________________________________________ > clisp-list mailing list > clisp-list@... > https://lists.sourceforge.net/lists/listinfo/clisp-list > -- Dan Becker