Does newLISP have &rest and big numbers...?

Started by ekd123, March 26, 2013, 07:10:51 AM

Previous topic - Next topic

ekd123

CL has a nice feature
(defun my-list (&rest list)
    list)
(my-list 'a 'b 'c 'd) => (a b c d)


Has newlisp got &rest? I haven't found anything about that. :-(



Also, big numbers are also very useful. It's here or not?



Thanks.



-- ekd123

cormullion

#1
No. For bigger numbers, see https://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers#Bigger_numbers">//https://en.wikibooks.org/wiki/Introduction_to_newLISP/Working_with_numbers#Bigger_numbers.



Don't know about &rest.

rickyboy

#2
Rest arguments in newLISP (by example):


> (define (f x y) (list x y (args)))

> (f 1 2 3 4 5 6)
(1 2 (3 4 5 6))
(λx. x x) (λx. x x)

ekd123

#3
Thanks. They worked with some hacks for gmp.lsp. (GMP in Fedora 64bit is located /usr/lib64/libgmp.so.10)



Well, another question. Has newLISP got &key and &optional? They are useful features, too.


;; Common Lisp example

(defun keylist (a &key x y z)
  (list a x y z))
(defun )
(keylist 1 :z 5 :y 3) => (1 NIL 3 5)

(defun optionallist (a &optional (x 5)) (list a x ))
(optionallist 6)

bairui

#4
newlisp has optional arguments (no need to point them out with a special keyword).

As I understand it, newlisp doesn't explicitly support keyword arguments although you could achieve a similar effect using association lists or hashes.

ekd123

#5
Thank you very much. Now I'm ready to do something in realworld =)

cormullion

#6
Shamelessly stealing from the creator:


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

> (foo (width 20) (height 30) (len 10))

len:10 width:20 height:30

rickyboy

#7
Cormullion's is a good approach.  Here is another.  I'm going to show it using ekd123's keylist definition.  Just say


(define-with-keys (keylist a)
  (list a x y z))

> (keylist 1 'z 5 'y 3)
(1 nil 3 5)

and you have the answer, but you have to use this "secret sauce."


(define-macro (define-with-keys name+params)
  (letex ($keylet (cons 'let (cons '$keybinds (args)))
          $name+params name+params)
    (define $name+params
      (letex ($keybinds (explode (args) 2))
        $keylet))))

Let's look at the value of keylist to see what define-with-keys does.


> keylist
(lambda (a)
 (letex ($keybinds (explode (args) 2))
  (let $keybinds
   (list a x y z))))

It assumes that (args) is keyword argument list.  That may be too simple for your use ...  or it may be "just right." :)  Happy hacking!
(λx. x x) (λx. x x)

ekd123

#8
WOW GR8! Thank you very much XD



Well, but is there a way to mix them up?

rickyboy

#9
How CL mixes these parameter types is discussed here by Seibel: http://www.gigamonkeys.com/book/functions.html#mixing-different-parameter-types">http://www.gigamonkeys.com/book/functio ... eter-types">http://www.gigamonkeys.com/book/functions.html#mixing-different-parameter-types.



I think it is possible to write a newLISP macro to mix the types.  Want to try?  :)
(λx. x x) (λx. x x)

ekd123

#10
I tried use them together and just found it successful.


(define-with-keys (together a (b nil)) (list a b c d))

(together 'a 'b 'c 1 'd 2) ; ==> (a b 1 2)


Just right!