Page 1 of 1

keyword parameters

Posted: Fri Nov 18, 2005 10:12 pm
by Dmi
I experiment with keyword arguments. Like :key in common lisp.

Code: Select all

(define-macro (keyfun _fn)
  (eval (append '(let) (list _args) _fn)))

(define (add-two)
  (let (_args (args))
    (keyfun
      (println (+ first-val second-val)))))
so:

Code: Select all

newLISP v.8.7.0 on linux, execute 'newlisp -h' for more info.

> (add-two 'first-val 1 'second-val 2)
3 
This makes possible constructs like:

Code: Select all

(send-email
  'from "me@here"
  'to "you@there"
  'body (format-message....))
(I reproduce some lisp example)

But can the syntax be more clear?
Something like:

Code: Select all

(define (add-two)
  (keyfun body))

or even:

(define-keyfun (fun-name) (fun-body))

Posted: Sat Nov 19, 2005 11:14 am
by Dmi
I was quite stupit yesterday... it's simple!

Code: Select all

(define-macro (keyfun)
  (eval (append '(let) (list _args) (args))))

(define-macro (define-keyfun _fnh)
  (set (_fnh 0)
       (append '(lambda) (list (1 _fnh))
               (list (append '(let (_args (args)))
                       (list (append '(keyfun) (args))))))))

(define-keyfun (test-val val)
  (print "testing " val " against " against "...")
  (println (unless (= val against) " not" "") " match"))

(test-val 1 'against 2)

Posted: Sat Nov 19, 2005 6:48 pm
by Fanda

Code: Select all

; This solution doesn't hide the implementation details. Good or bad?

(define (test)
  (let (x nil y nil)
    (apply set (args))
    (println "x = " x " y = " y)))


(test 'x 1 'y 2)  ; => x = 1 y = 2
(test 'y 2 'x 1)  ; => x = 1 y = 2
Fanda

Posted: Sat Nov 19, 2005 6:53 pm
by Dmi
Yes! It's that I really looking for :-)
Thanks.