Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:50:18+00:00 2026-06-05T07:50:18+00:00

I’m having issues trying to form code for a problem I want to resolve.

  • 0

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

  1. If the object is a list, replace the list with the sum of its atoms.
  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:50:21+00:00Added an answer on June 5, 2026 at 7:50 am

    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?

    (defun condense (x)
      (typecase x
        (number  
           ; then what?
           (condense-number x))
        (list
           ; then what?
           (if (all-atoms x)
             (condense-list-of-atoms x) ; how to do that?
             (process-further-somehow
                (condense-lists-inside x))))
        ; what other clauses, if any, must be here?
        ))
    

    What must condense-lists-inside do? 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,

    (defun condense-lists-inside (xs)
      (mapcar #'dowhat xs))
    

    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-atoms check.

    edit: actually, using typecase was 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 function calculate that does that, no need to go so far back as to condense itself. When you substitute calculate there, it will become evident that the check for all-atoms is 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-atoms check will break your requirement #2. The calculation will then proceed as follows

    (CONDENSE '(2 3 4 (3 1 1 1) (2 3 (1 2)) 5))
    ==
    (calculate (mapcar #'condense '(2 3 4 (3 1 1 1) (2 3 (1 2)) 5)))
    == 
    (calculate (list 2 3 4 (condense '(3 1 1 1)) (condense '(2 3 (1 2))) 5))
    == 
    (calculate (list 2 3 4 (calculate '(3 1 1 1)) 
                             (calculate (list 2 3 (calculate '(1 2)))) 5))
    == 
    (calculate (list 2 3 4 6 (calculate '(2 3 3)) 5))
    == 
    (calculate (list 2 3 4 6 8 5))
    ==
    28
    

    I.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-atoms check would proceed strictly by the levels up.

    So the final simplified code is:

    (defun condense (x)
      (if (listp x)
        (reduce #'+ (mapcar #'condense x))
        (abs x)))
    

    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 reduce is.

    This can be directly coded with what’s known as “car-cdr recursion”, replacing each cons cell with an application of a combining function f on two results of recursive calls into car and cdr components of the cell:

    (defun condense (x) (reduce-tree x #'+ 0))
    (defun reduce-tree (x f z)
      (labels ((g (x)
                (cond
                 ((consp x) (funcall f (g (car x)) (g (cdr x))))
                 ((numberp x) x)
                 ((null x) z)
                 (T (error "not a number")))))
        (g x)))
    

    As you can see this version is highly recursive, which is not that good.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have some data like this: 1 2 3 4 5 9 2 6
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.