Page 1 of 1

pushing stuff

Posted: Fri Jan 13, 2006 12:40 pm
by tom
howdy guys,

if I have something like,

Code: Select all

(set 'a '())
(push this a)
(push that a)
how can I do, instead,

Code: Select all

(push this that a)
I'm not getting it to work.

Posted: Fri Jan 13, 2006 1:28 pm
by cormullion
Does append work?

(set 'a (append a (list this) (list that)))

- doesn't look quite as nice ...

Posted: Fri Jan 13, 2006 1:42 pm
by newdep
(push "hello" a -1) perhpas?

Posted: Fri Jan 13, 2006 4:18 pm
by Sammo
You could write your own version of push -- mypush-- that would be used something like this:

Code: Select all

(mypush 1 2 3 'a)
Notice that I quote the stack (in this case 'a) to prevent it from being evaluated.

Something like this (this code doesn't work):

Code: Select all

(define (mypush)
  (let
    ( stack (last (args))
      list-of-items (butlast (args))
    )
  ;body of let
    (dolist (item list-of-items)
      (push item (eval stack)) )))
Since newLisp doesn't have the 'butlast' function (anybody got a good one?), I'll hack it together like this (this code works):

Code: Select all

(define (mypush)
  (let
    ( stack (last (args))
      list-of-items (reverse (rest (reverse (args))))
    )
  ;body of let
    (dolist (item list-of-items)
      (push item (eval stack) -1)) ))
I'm sure someone (Lutz?) will suggest improvements.

An alternate solution would use define-macro for a syntax more like the built-in 'push'.

Edit: I figured out that 'butlast' is easily defined with chop:

Code: Select all

;; butlast
;; return all but last n (default 1) elements of list / chars of string
;;
(define (butlast list-or-string n)
  (chop list-or-string (or n 1)) )

Use map.

Posted: Fri Jan 13, 2006 8:02 pm
by ghfischer

Code: Select all

> (set 'a '())
() 
> (set 'b '("this" "that"))
("this" "that") 
> (map (fn(x) (push x a)) b)
("this" "that") 
> a
("that" "this") 
This will push every element in b onto a.