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 3681632
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:42:33+00:00 2026-05-19T03:42:33+00:00

First, I should make it clear that this is required for an academic project.

  • 0

First, I should make it clear that this is required for an academic project. I am trying to find the maximum number of child nodes for any node in a tree, using Common Lisp.

My current code is shown below – I’m not 100% on the logic of it, but I feel it should work, however it isn’t giving me the required result.

(defun breadth (list y)
  (setf l y)
        (mapcar #'(lambda (element) 
              (when (listp element) 
         (when (> (breadth element (length element)) l) 
             (setf l (breadth element (length element)))
           ))) list)

l)

(defun max-breadth(list)
  (breadth list (length list))
  )

As an example, running

(max-breadth '(a ( (b (c d)) e) (f g (h i) j)))

should return 4.

Edit:
Trace results and actual return values, forgot these:

CG-USER(13): (max-breadth '(a ( (b (c d)) e) (f g (h i) j)))
 0[6]: (BREADTH (A ((B (C D)) E) (F G (H I) J)) 3)
   1[6]: (BREADTH ((B (C D)) E) 2)
     2[6]: (BREADTH (B (C D)) 2)
       3[6]: (BREADTH (C D) 2)
       3[6]: returned 2
     2[6]: returned 2
   1[6]: returned 2
   1[6]: (BREADTH (F G (H I) J) 4)
     2[6]: (BREADTH (H I) 2)
     2[6]: returned 2
   1[6]: returned 2
 0[6]: returned 2
2

Does anyone have any ideas where I’m going wrong? I suspect it’s related to the second conditional, but I’m not sure.

  • 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-05-19T03:42:33+00:00Added an answer on May 19, 2026 at 3:42 am

    First, standard formatting:

    (defun breadth (list y)
      (setf l y)
      (mapcar #'(lambda (element) 
                  (when (listp element) 
                    (when (> (breadth element (length element)) l) 
                      (setf l (breadth element (length element))))))
              list)
      l)
    
    (defun max-breadth (list)
      (breadth list (length list)))
    

    Your problem is the (setf l y), which should give you a warning about l being undefined. Setf should not be used on unbound variables. Use let to make a lexical scope:

    (defun breadth (list y)
      (let ((l y))
        (mapcar #'(lambda (element) 
                    (when (listp element) 
                      (when (> (breadth element (length element)) l) 
                        (setf l (breadth element (length element))))))
                list)
        l))
    

    Then, instead of two nested when, use a single one and and:

                  (when (and (listp element)
                             (> (breadth element (length element)) 1))
                    (setf l (breadth element (length element))))
    

    I find dolist more concise here:

      (dolist (element list)
        (when (and (listp element)
                   (> (breadth element (length element)) l))
          (setf l (breadth element (length element)))))
    

    The parameter y is always the length of the parameter list, so this call can be simplified. You also do not need to alias y:

    (defun breadth (list &aux (y (length list)))
      (dolist (element list)
        (when (and (listp element)
                   (> (breadth element) y))
          (setf y (breadth element))))
      y)
    

    You could eliminate the double recursive call through a let, but we can use max here:

    (defun breadth (list &aux (y (length list)))
      (dolist (element list)
        (when (listp element)
          (setf y (max y (breadth element)))))
      y)
    

    You could also use reduce for this:

    (defun breadth (l)
      (if (listp l)
          (reduce #'max l
                  :key #'breadth
                  :initial-value (length l))
          0))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I need to make it clear - this is for an
First check out this code. I seems like it should work for me, but
First off, let me say that this is not homework (I am an A-Level
Been a tough week, will try to make this as clear as possible. Appreciate
I had a requirement where in the text field the first character should be
[1] In JDBC, why should we first load drivers using Class.forName(some driver name). Why
What should a small team choose for their first game, when they are low
What single aspect of agile development should we implement first to improve our development
Example: I have two shared objects (same should apply to .dlls). The first shared
I'm working on my first real MVC application and I'm trying to follow general

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.