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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:42:22+00:00 2026-05-23T09:42:22+00:00

I would like to go through a list (which may have nested lists) and

  • 0

I would like to go through a list (which may have nested lists) and have it evaluate to one flattened list with all of the elements. I can’t even get a recursive function to evaluate to anything other than nil

(defun pl(lst)
  (if (atom lst)
      lst
    (progn
      (pl (car lst))
      (pl (cdr lst)))))

I’ll then call with something like (pl '(1 (2 3) (4 5))), but it always evaluates to nil.

I changed

(if (atom lst) lst

to

(if (atom lst) (print lst)

and this doesn’t even print any of the items of the list.

what concept I am missing here?

  • 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-23T09:42:23+00:00Added an answer on May 23, 2026 at 9:42 am

    A function will normally only return one value, which is value of its body. If you look closely at pl you see that it is an if form, so pl either returns the value of lst or the value of progn.

    The first thing I have to point out here is that the return value of a (progn ...) form is the value of its last expression, which in this case is the recursive call (pl (cdr lst)). As you do not do anything with the return value of (pl (car lst)) this call has no effect whatsoever. The value of the recursive call will at some point fall through the atom test. The second thing to point out here is that (atom nil) is also true. Remember that the last element of a list is nil, so when you give pl a list it will always return nil as you have observed.

    If your print version shows nothing, it is probably because the print output is shown somewhere else, in another buffer for example.

    As for the solution: you either want a pure recursive solution by using append instead of progn, because that’s what in your homework assignment. In regular lisp you just use one of the iteration constructs.

    My advice is to check any textbook on lisp or scheme to grasp the fundamentals of recursion and tail-recursion.

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

Sidebar

Related Questions

I would like to cycle through four li elements that all contain tags, setting
I would like to be able to loop through a list which is a
Does map() iterate through the list like for would? Is there a value in
I would like to search through all of my procedures packages and functions for
When a select field is changed, I would like to loop through all of
I would like to be able to loop through all of the defined parameters
I have an application (cad like editor) in which the user may place a
I have a number of operations on data frames which I would like to
I am looping through some unordered lists and I'd like to get all of
I would like to step through dataset designer code in an ASP.NET project. How

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.