我可以执行以下操作:
> set one-deep [[3] [3] [1] [3] [2]]
> reduce sentence one-deep
: [3 3 1 3 2]
但是我如何展平更深的列表列表...列表呢?
> set two-deep [[1] [3] [1] [2] [[1] [1] [2] [3] [3]] ]
???
: [1 3 1 2 1 1 2 3 3]
答案 0 :(得分:4)
如果您确定列表的深度只有两层,则可以执行以下操作:
reduce sentence reduce sentence two-deep
但是,如果您需要更通用的解决方案,则应该可以执行以下操作:
to-report unnest [ xs ]
let ys reduce sentence xs
report ifelse-value (reduce or map is-list? ys) [ unnest ys ] [ ys ]
end
(reduce or map is-list? ys)
表达式大量使用了NetLogo简洁的匿名过程语法,但这仅表示“ ys
中的任何项目都是列表吗?”