Post Reply 
How to write a program to output multiple results
10-26-2020, 06:54 PM
Post: #9
RE: How to write a program to output multiple results
(10-26-2020 01:23 PM)pinkman Wrote:  Yes good approach, but there is no flatten function on the Prime.
We should request it ! Big Grin

I was looking at XCas source misc.cc, I discovered there is an undocumented flatten1

XCas> flatten1([1,2,3,[4,[5]]])       → [1,2,3,4,[5]]

The code also explained why flatten is so fast.
It only scan the list 1 time, adding 1 element at a time, with time complexity of O(n)

We could emulate the functionality of flatten, but time complexity is O(n^2)
Here is a simple implementation. (note that concat is needed, which kills performance.)
Code:
flat(lst) := {
  local h := head(lst);
  return (h == lst) ? h : concat(flat(h), flat(tail(lst)));
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: How to write a program to output multiple results - Albert Chan - 10-26-2020 06:54 PM



User(s) browsing this thread: 4 Guest(s)