Re: optional arguments in CLush functions?

Yann LeCun <[email protected]>
Newsgroups gmane.lisp.lush.devel
Organization Courant Institute, NYU
Message-ID <[email protected]>
Allowing null pointers to be tested like this in C-lush
would be horribly unsafe.

Thankfully, there is a simple solution to your problem
by using macros (thank you lisp.gods):

================================================================
(dm my-plus (dummy a b . cl)
  (if cl
      `(do-stuff ,a ,b ,(car cl))
    `(let ((c (clone-array ,a))) (do-stuff ,a ,b c))))

(printf "with 2 arguments:\n")
(pretty (macro-expand (my-plus x y)))

(printf "\nwith 3 arguments:\n")
(pretty (macro-expand (my-plus x y z)))
================================================================

Loading the above code produces this:

with 2 arguments:
(let ((c (clone-array x)))
  (do-stuff x y c) )

with 3 arguments:
(do-stuff x y z)


  -- Yann


On Tuesday 15 November 2005 07:56 pm, Ralf Juengling wrote:
> Hi,
>
> When I write C functions where the arguments are objects
> (pointers) and want to make some of the arguments optional,
> an easy way to do so is by the convention that NULL pointers
> indicate absent arguments.
>
> I'd like to do something similar with CLush. For the sake
> of an example, say I might want to write my own vector copying
> routine and if three args are provided, then the last arg is
> taken to be the storage for the result.
>
> (defun my-plus (a b c)
>    ((-idx1- (-double-)) a b c)
>
>    (if (= (to-gptr c) ())
>        (let ((c (clone-array a)))
>          ;; do stuff
>          c)
>      ;; do other stuff and use input c
>      c))
>
> With two args the invocation would look like
>
> (my-plus u v NULL)  ;; NULL some new symbol
>
> with three args it would look like
>
> (my-plus u v w)
>
>
> Of course, it doesn't work currently--at least not the
> way I tried. There are a number of questions here:
>
> 1. Is there any way, currently, to do what I want to do?
>
> 2. When you try to create a NULL ptr (a gptr object with
>     value NULL) in lush, the result is nil. When the CLush
>     compiler encounters a nil, it considers it a boolean
>     value rather than a NULL pointer.
>     Maybe we need to add a new symbol for this purpose to
>     lush/CLush, |NULL| say?
>
> 3. Would it be possible to change the CLush compiler to
>     make it accept a NULL ptr as arguments where the
>     corresponding formal parameter is an object?
>
> Any comments appreciated.
> Thanks,
> Ralf
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
> Register for a JBoss Training Course.  Free Certification Exam
> for All Training Attendees Through End of 2005. For more info visit:
> http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
> _______________________________________________
> Lush-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/lush-devel




-------------------------------------------------------
This SF.Net email is sponsored by the JBoss Inc.  Get Certified Today
Register for a JBoss Training Course.  Free Certification Exam
for All Training Attendees Through End of 2005. For more info visit:
http://ads.osdn.com/?ad_id=7628&alloc_id=16845&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.