I’m having issues trying to form code for a problem I want to resolve. It goes like this:
~ Goal: flatten a nested list into one number
- If the object is a list, replace the list with the sum of its atoms.
- With nested lists, flatten the innermost lists first and work from there.
Example:
(CONDENSE '(2 3 4 (3 1 1 1) (2 3 (1 2)) 5))
(2 3 4 (6) (2 3 (3)) 5)
(2 3 4 (6) (8) 5)
(28)
=> 28
I’ve tried to implement the flatten list function for this problem and I ended up with this:
(defun condense (lst)
(cond
((null lst) nil)
((atom lst) (list lst)))
(t (append (flatten (apply #'+ (cdr lst))))))
But it gives me errors 🙁
Could anyone explain to me what is wrong with my processing/code? How can I improve it?
UPDATE: JUNE 5 2012
(defun condense(lxt)
(typecase lxt
(number (abs lxt))
(list
(if (all-atoms lxt)
(calculate lxt)
(condense (mapcar #'condense lxt))))))
So here, in this code, my true intent is shown. I have a function calculate that performs a calculation based off the values in the list. It is not necessarily the same operation each time. Also, I am aware that I am returning the absolute value of the number; I did this because I couldn’t find another way to return the number itself. I need to find a way to return the number if the lxt is a number. And I had it recurse two times at the bottom, because this is one way that it loops on itself infinitely until it computes a single number. NOTE: this function doesn’t implement a flatten function anymore nor does it use anything from it.
Imagine you have your function already. What does it get? What must it produce?
Given an atom, what does it return? Given a simple list of atoms, what should it return?
What must
condense-lists-insidedo? According to your description, it is to condense the nested lists inside – each into a number, and leave the atoms intact. So it will leave a list of numbers. To process that further somehow, we already “have” a function,condense-list-of-atoms, right?Now, how to implement
condense-lists-inside? That’s easy,Do what? Why,
condense, of course! Remember, we imagine we have it already. As long as it gets what it’s meant to get, it shall produce what it is designed to produce. Namely, given an atom or a list (with possibly nested lists inside), it will produce a number.So now, fill in the blanks, and simplify. In particular, see whether you really need the
all-atomscheck.edit: actually, using
typecasewas an unfortunate choice, as it treats NIL as LIST. We need to treat NIL differently, to return a “zero value” instead. So it’s better to use the usual(cond ((null x) ...) ((numberp x) ...) ((listp x) ...) ... )construct.About your new code: you’ve erred: to process the list of atoms returned after
(mapcar #'condense x), we have a functioncalculatethat does that, no need to go so far back as tocondenseitself. When you substitutecalculatethere, it will become evident that the check forall-atomsis not needed at all; it was only a pedagogical device, to ease the development of the code. 🙂 It is OK to make superfluous choices when we develop, if we then simplify them away, after we’ve achieved the goal of correctness!But, removing the
all-atomscheck will break your requirement #2. The calculation will then proceed as followsI.e. it’ll proceed in left-to-right fashion instead of the from the deepest-nested level out. Imagining the nested list as a tree (which it is), this would “munch” on the tree from its deepest left corner up and to the right; the code with
all-atomscheck would proceed strictly by the levels up.So the final simplified code is:
a remark: Looking at that last illustration of reduction sequence, a clear picture emerges – of replacing each node in the argument tree with a calculate application. That is a clear case of folding, just such that is done over a tree instead of a plain list, as
reduceis.This can be directly coded with what’s known as “car-cdr recursion”, replacing each
conscell with an application of a combining functionfon two results of recursive calls intocarandcdrcomponents of the cell:As you can see this version is highly recursive, which is not that good.