keyword parameters

For the Compleat Fan
Locked
Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

keyword parameters

Post 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))
WBR, Dmi

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post 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)
WBR, Dmi

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post 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

Dmi
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Yes! It's that I really looking for :-)
Thanks.
WBR, Dmi

Locked