# (lispkit stack)

Library `(lispkit stack)` provides an implementation for mutable stacks, i.e. mutable LIFO buffers.

**stack-type-tag** <img src="/files/viHiVHsCY8Wn2TeCgWJj" alt="" data-size="line">

Symbol representing the `stack` type. The `type-for` procedure of library `(lispkit type)` returns this symbol for all stack objects.

**(make-stack)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a new empty stack.

**(stack&#x20;*****x ...*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a new stack with *x* on its top position followed by the remaining parameters.

```scheme
(stack-top (stack 1 2 3))  ⇒ 1 
```

**(stack?&#x20;*****obj*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if *obj* is a stack; otherwise `#f` is returned.

**(stack-empty?&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if stack *s* is empty.

**(stack-size&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the size of stack *s*, i.e. the number of elements buffered in *s*.

**(stack=?&#x20;*****s1 s2*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns `#t` if stack *s1* has the exact same elements in the same order like stack *s2*; otherwise, `#f` is returned.

**(stack-push!&#x20;*****s x*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Pushes element *x* onto stack *s*.

**(stack-top&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns the top element of stack *s*. If the stack is empty, an error is raised.

**(stack-pop!&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Removes the top element from stack *s* and returns its value.

```scheme
(define s (make-stack))
(stack-push! s 1)
(stack-push! s 2)
(stack-pop! s)  ⇒ 2
(stack-size s)  ⇒ 1
```

**(stack-clear!&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Removes all elements from stack *s*.

**(stack-copy&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a copy of stack *s*.

**(stack->list&#x20;*****s*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a list consisting of all elements on stack *s* in the order they appear, i.e. starting with the top element.

```scheme
(stack->list (stack 1 2 3))
```

**(list->stack&#x20;*****l*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Returns a new stack consisting of the elements of list *l*. The first element in *l* will become the top element of the stack that is returned.

**(list->stack!&#x20;*****s l*****)** <img src="/files/STqjiJsrexexyFklGQwH" alt="" data-size="line">

Pushes the elements of list *l* onto stack *s* in reverse order.

```scheme
(define s (list->stack '(3 2 1)))
(list->stack! s '(6 5 4))
(stack->list s)  ⇒ (6 5 4 3 2 1)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.lisppad.app/libraries/lispkit/lispkit-stack.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
