Reputation: 11
I am trying to solve this problem from the 99 lisp problems
Flatten a nested list structure.
Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively).
Example:
(my-flatten '(a (b (c d) e)))
(A B C D E)
Hint: Use the predefined functions list and append.
Problem is, I don't know how to create that nested list to begin with so I can try to flatten it myself. Please help thankyou
Upvotes: 1
Views: 574
Reputation: 28950
In Lua "a list possibly holding lists as elements" is just a table possibly holding tables as elements.
From the Lua manual:
Tables can be heterogeneous; that is, they can contain values of all types (except nil).
...
Like indices, the values of table fields can be of any type
That of course includes tables.
The most trivial case is local t = {{}}
or for the mentioned example (a (b (c d) e)) local t = {a, {b, {c, d}, e}}
Assuming a,b,c,d and e are strings your list would look like so:
local t = {"a", {"b", {"c", "d"}, "e"}
Upvotes: 1