Search code examples
maximawxmaxima

(wx)Maxima: determining the number of parts of an expression


I'd like to use part to handle expressions of different length but have not been able to find anything in the documentation that addresses how to determine the number of parts of an expression.

I do have an upper bound for the number of parts so, in this particular case, I could loop over the terms until I get an error; however, I was wondering if there is a more direct method?


Solution

  • I can't believe this, but it appears length does the trick.

    I assumed it was limited to lists because it returns an error for single numbers, because they are atoms. Apparently my brain decided "doesn't work with atoms" to mean "only work with lists."

    However, this does mean that neither part nor length will work if the expression only has one part, so that case has to be handled separately.

    I would still be interested in knowing if there is a solution that will work in all cases, so I won't mark this as the answer, just yet.