pushing stuff

Started by tom, January 13, 2006, 04:40:36 AM

Previous topic - Next topic

tom

howdy guys,



if I have something like,

(set 'a '())
(push this a)
(push that a)


how can I do, instead,

(push this that a)


I'm not getting it to work.

cormullion

#1
Does append work?



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



- doesn't look quite as nice ...

newdep

#2
(push "hello"  a -1) perhpas?
-- (define? (Cornflakes))

Sammo

#3
You could write your own version of push -- mypush-- that would be used something like this:
(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):
(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):
(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:
;; 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)) )

ghfischer

#4

> (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.