Flatten a list: Difference between revisions
Content added Content deleted
(added Quackery) |
(adding lambdatalk) |
||
Line 2,291: | Line 2,291: | ||
Flattened : [1, 2, 3, 4, 5, 6, 7, 8] |
Flattened : [1, 2, 3, 4, 5, 6, 7, 8] |
||
</pre> |
</pre> |
||
=={{header|Lambdatalk}}== |
|||
Lambdatalk doesn't have a builtin primitive flattening a multidimensionnal array. |
|||
<lang scheme> |
|||
1) Let's create this function |
|||
{def A.flatten |
|||
{def A.flatten.r |
|||
{lambda {:a} |
|||
{if {A.empty? :a} |
|||
then |
|||
else {let { {:b {A.first :a}} |
|||
} {if {A.array? :b} |
|||
then {A.flatten.r :b} |
|||
else :b} } |
|||
{A.flatten.r {A.rest :a}} }}} |
|||
{lambda {:a} |
|||
{A.new {A.flatten.r :a}}}} |
|||
-> A.flatten |
|||
and test it |
|||
{def list |
|||
{A.new |
|||
{A.new 1} |
|||
2 |
|||
{A.new {A.new 3 4} 5} |
|||
{A.new {A.new {A.new }}} |
|||
{A.new {A.new {A.new 6}}} |
|||
7 |
|||
8 |
|||
{A.new} |
|||
}} |
|||
-> [[1], 2, [[3, 4], 5], [[[]]], [[[6]]], 7, 8, []] |
|||
{A.flatten {list}} |
|||
-> [1,2,3,4,5,6,7,8] |
|||
</lang> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |