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

  • SEARCH
  • Home
  • 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 383263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:15:57+00:00 2026-05-12T15:15:57+00:00

I have been listening to Stanford’s programming paradigm lecture series , but I’m confused

  • 0

I have been listening to Stanford’s programming paradigm lecture series, but I’m confused by the following code (from lecture 20). Would someone explain, line by line, what this is doing?

Thanks.

(define (flatten sequence)
  (cond ((null? sequence) '())
        ((list? (car sequence)) (append (flatten (car sequence))
                                        (flatten (cdr sequence))))
        (else (cons (car sequence)
                    (flatten (cdr sequence))))))
  • 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-12T15:15:57+00:00Added an answer on May 12, 2026 at 3:15 pm
    # define a procedure 'flatten' that takes a list 'sequence'
    (define (flatten sequence)
      # if the 'sequence' is empty, return an empty list
      (cond ((null? sequence) (list))
            # if the first element of 'sequence' is itself a list, return a new list
            # made by appending the flattened first element of 'sequence' with the
            # flattened rest of the 'sequence'
            ((list? (car sequence))
             (append (flatten (car sequence))
                     (flatten (cdr sequence))))
            # if the first element of 'sequence' is not a list, return a new list
            # made by cons-ing that element with the flattened rest of the 'sequence'
            (else
             (cons (car sequence)
                   (flatten (cdr sequence))))))
    

    I pretty-printed it (inserted some newlines and indented the code to show its structure) and also replaced '() with (list) (which has the same value) just to prevent the code from being highlighted incorrectly.

    +1 what is cons-ing? I’d appreciate if you explain other keywords as well. thanks

    When I say cons-ing I’m just referring to the cons procedure. When you see (cons <expression> <list>) where <expression> is any Scheme expression or value and <list> is any Scheme expression that evaluates to a list, cons will return the <list> with the value of the <expression> tacked on the front of it. For example, (cons 1 (list 2 3 4)) returns the list (list 1 2 3 4). In fact, (list 1 2 3 4) in Scheme is just a short way of writing (cons 1 (cons 2 (cons 3 (cons 4 '() )))).

    The other two words you might have trouble with are car and cdr. You can think of (car <list>) as meaning the first element or head of the <list>, and (cdr <list>) as meaning the rest of the elements or tail of the <list>. For example, (car (list 1 2 3 4)) returns the value 1, and (cdr (list 1 2 3 4)) returns the list (list 2 3 4).

    If you need any help with other keywords, let me know.

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

Sidebar

Related Questions

I have been studying the bonjour/NSStream sample code from lecture #17 of Stanford's CS193p
I was listening to a podcast recently (may have been SO - can't remember)
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
Have been searching how to convert a dictionary to a string. But the results
I'm on a Linux Ubuntu 12.04 system. I have been using this code to
I am implementing a server in Python. I have been following the tutorial on
I know this might have been asked a thousand times before, but I can't
Have been trying to encrypt an xml file to a string so that I
Have been working on this question for a couple hours and have come close
Have been reading about async and tasks and been attempting to convert the CopyFileEx

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.