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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:00:18+00:00 2026-05-26T15:00:18+00:00

Here is the question: Write a function (first-n-elements lst n) , which returns a

  • 0

Here is the question:

Write a function (first-n-elements lst n), which returns a list containing only the first n elements of lst. For example, (first-n-elements '(1 2 3 4 5 6) 3) should return '(1 2 3). Your function should handle the case where n is greater than the length of the list (in which case it would return the entire list), and where n is 0 (should return '()).

My answer is:

(define (first-n-elements lst n)
  (cond((null? lst) '())    
       ((= n 0) lst))   
       ((> n 0) (cons (+ (car lst) 1) (first-n-elements) (cdr lst) (- n 1))))

I know it’s wrong, please help

  • 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-26T15:00:18+00:00Added an answer on May 26, 2026 at 3:00 pm

    Part of the question says that if n is 0, it should return '(). But what does your function do when n is 0?

    Also, think about the recursive case here (> n 0). You’re currently returning the cons of four arguments:

    • (+ (car lst) 1)
    • (first-n-elements)
    • (cdr lst)
    • (- n 1)

    But cons only takes two arguments. You have all the parts there, but they’re not quite put together right. What do you want to be consing together?

    Also: why are you adding 1 to (car lst)? That’s not going to work if lst has something other than numbers in it.

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

Sidebar

Related Questions

A nice C interview question: Can you write a function which swaps two int*
Here is the question: write a method that swaps two variables. These two variables
first question here. I'm developing a program in C# (.NET 3.5) that displays files
First question here: it is a very short yet fundamental thing in Java that
First question here so hello everyone. The requirement I'm working on is a small
i've asked the first question about selects here , i thought, that when i
Since this is my first question here on stackoverflow I hope my question is
This is my first question here on stackoverflow, so I hope that I am
First - a short description of a problem. I write a JavaScript function to
I am trying to write my first real python function that does something real.

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.