SCHEME from list to its values

Viewed 60

How can I convert this list

(define l list '(1 2 3 4) )

to its own value 1, 2, 3 and 4

I need to do this because I have a function

(define (push!  stk . args)
(stk 'push! args ) ) 

for this other function

((eq? msg 'push!) (set! stack (append (reverse args) stack)))

but the result I get when using push! is a list in a list, I don't want this

2 Answers

You can by iteration. eg. fold:

(fold cons '() '(1 2 3 4))
; ==> (4 3 2 1)

Now the most stackie functional structure is the lisp list since it is a singly linked list that constructs from end to beginning while iterates from beginning to end.

Go the other way instead - gather all the arguments in one list, and then apply:

(apply push! (cons the-stack l))
Related