functions parameters

Q&A's, tips, howto's
Locked
kib2
Posts: 9
Joined: Fri Nov 07, 2008 3:33 pm
Location: France
Contact:

functions parameters

Post by kib2 »

Hi,

I just wanted to know how to handle functions parameters in newLisp, in particular those ones (I only saw optionnal parameters in the docs):

- rest parameters;
- keyword parameters;

Thanks.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

&rest and &optional like CL could used like (args) in newlisp like (define (foo a b) (args)) but you have to build around it al ittle yourself..
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You can implement named parameters this way:

Code: Select all

(define-macro (foo)
    (local (len width height)
        (bind (args) true)
        (println "len: " len " width:" width " height:" height)
))      

(foo (width (+ 15 5)) (height 30) (len 10))

; this prints

len: 10 width:20 height:30
Arguments are evaluated and assigned to the local variables.

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

Very elegant solution.

kib2
Posts: 9
Joined: Fri Nov 07, 2008 3:33 pm
Location: France
Contact:

Post by kib2 »

Thanks for all your replies,

newdep : &rest and &optional were exactly what I was looking for. Now I need to test "args" within newLisp.

Lutz : this is a great example of a macro use : thanks for it !

Do you think such a markup langage http://www.nongnu.org/skribilo/ can be implemented in newLisp? In fact, that's what I want to do as a first project (with less features), but maybe I'm too pretentious/ambitious.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I often ask for named parameters too :-) (http://www.alh.net/newlisp/phpbb/viewtopic.php?t=2180) There are many ways of implementing it - I just usually forget them when I need them...

skribilo looks interesting - but a substantial project too.

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

Lutz wrote:You can implement named parameters this way:

Code: Select all

(define-macro (foo)
    (local (len width height)
        (bind (args) true)
        (println "len: " len " width:" width " height:" height)
))      

(foo (width (+ 15 5)) (height 30) (len 10))

; this prints

len: 10 width:20 height:30
Arguments are evaluated and assigned to the local variables.
Lutz, could you add this to "Code Patterns" section 4. Local variables?

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

Locked