我对 OpenSCAD 还很陌生,但大部分内容都已完成。但是,我不确定如何进行类似堆栈的操作。只要我可以推送和弹出数字,使用什么数据结构并不重要。这可能吗?
1 回答
1
I worked extensively with stacks while implementing a string processing library.
The trick is to use nested right associative lists of size = 2, e.g. ["foo", ["bar", []]].
If you want to push something to a stack:
function push(stack, item) = [item, stack];
If you want to pop the stack:
function pop(stack) = stack[1];
And if you want to peek or retrieve the value that was popped:
function peek(stack) = stack[0];
You can also implement map/reduce functions using recursion:
function map(stack) =
push(
map(pop(stack)),
f(peek(stack))
);
function reduce(stack) =
f(peek(stack))?
push(
reduce(pop(stack)),
f(peek(stack))
)
:
reduce(pop(stack))
;
Of course, now that version 2015.03 is out you might consider using list comprehension, assuming that's what you really need.
于 2015-04-16T14:45:22.673 回答