## 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 ```
 Re: [clisp-list] beginner questions From: Mark Volkmann - 2011-01-19 01:09:11 ```Thanks Dan! Suppose I don't specify default values for my optional arguments. Here's another solution that works. Do you think there's anything odd about this approach? My goal is really just to learn about all the ways to specify function parameters. (defun opt-args (a b &optional c d) (apply #'+ (remove-if #'null (list a b c d)))) On Mon, Jan 17, 2011 at 9:33 PM, Dan Becker wrote: > 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 > -- R. Mark Volkmann Object Computing, Inc. ```
 Re: [clisp-list] beginner questions From: Lam Luu - 2011-01-19 01:12:52 ```I think, if you have already used apply, and if you don't have restriction on the number of argument, something like this is easier: (defun opt-args (&rest everything) (apply #'+ everything)) Have fun, Lam Luu On 01/18/2011 07:09 PM, Mark Volkmann wrote: > Thanks Dan! > > Suppose I don't specify default values for my optional arguments. > Here's another solution that works. > Do you think there's anything odd about this approach? > My goal is really just to learn about all the ways to specify function > parameters. > > (defun opt-args (a b &optional c d) > (apply #'+ (remove-if #'null (list a b c d)))) > > On Mon, Jan 17, 2011 at 9:33 PM, Dan Becker wrote: >> 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 >> > > ```
 Re: [clisp-list] beginner questions From: Sam Steingold - 2011-01-25 15:32:12 ```> * Mark Volkmann [2011-01-18 19:09:03 -0600]: > > Do you think there's anything odd about this approach? > My goal is really just to learn about all the ways to specify function > parameters. > > (defun opt-args (a b &optional c d) > (apply #'+ (remove-if #'null (list a b c d)))) this should work. note that this is also equivalent to (defun opt-args (a b &optional c d) (apply #'+ (remove nil (list a b c d)))) actually, since you are already creating the list yourself, you can use DELETE instead of REMOVE to avoid extra consing. http://www.lispworks.com/documentation/HyperSpec/Body/f_rm_rm.htm -- Sam Steingold (http://sds.podval.org/) on CentOS release 5.3 (Final) http://mideasttruth.com http://memri.org http://thereligionofpeace.com http://www.memritv.org http://www.PetitionOnline.com/tap12009/ Profanity is the one language all programmers know best. ```
 Re: [clisp-list] beginner questions From: Mark Volkmann - 2011-01-19 01:52:25 ```On Tue, Jan 18, 2011 at 7:31 PM, Kaz Kylheku wrote: > > On Tue, 18 Jan 2011 19:09:03 -0600, Mark Volkmann > wrote: >> Thanks Dan! >> >> Suppose I don't specify default values for my optional arguments. >> Here's another solution that works. >> Do you think there's anything odd about this approach? > > Yes; what is odd is not doing the obvious thing (supply the > default zero values), and compensating for that by consing. :) I know. I was just trying to write an example that mixed required and optional parameters. >> My goal is really just to learn about all the ways to specify function >> parameters. > > But you already know about optional parameters without default values. > > A more interesting step might be to learn about the third item that may > be > specified for an optional parameter: a boolean variable which > indicates whether or not the caller supplied a parameter. > This extra boolean distinguishes the case when the caller passed > a parameter which matches the default value, from the case > when the parameter actually defaulted. > > (defun foo (&optional (bar 42 bar-p)) >  (list bar bar-p)) > > (foo) -> (42 NIL) > (foo 42) -> (42 T) > (foo 3) -> (3 T) That IS more interesting. Thanks! >> (defun opt-args (a b &optional c d) >>   (apply #'+ (remove-if #'null (list a b c d)))) > > This is very similar to: > >  (defun opt-args (&rest args) >   (apply #'+ args)) > > with the added restriction there must be two, three or four arguments. > > The restriction isn't very useful because it just means that the > function deliberately breaks when called with one, zero or more > than four arguments for no good reason. > > When several optional arguments behave like a sequence of similar > things, ask whether the function doesn't naturally generalize > to taking any number of such arguments? I agree, but I was trying to mix required and optional parameters. My example of what to do with those parameters was bad for the reasons you stated. -- R. Mark Volkmann Object Computing, Inc. ```

JavaScript is required for this form.

No, thanks