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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:04:40+00:00 2026-06-15T09:04:40+00:00

I’m trying to use recursion in the list and I need to go through

  • 0

I’m trying to use recursion in the list and I need to go through all elements. Here is my code:

(define compare
  (lambda (ls pred?)
    (if (null? list)
        #f
        (pred? (list-ref ls (- (length ls) 2)) (list-ref ls (- (length ls) 1))))))

But it works only with last two elements. Result should be like this:

(compare '(1 2 3 4 5) <) -> #t
(compare '(1 2 8 4 5) <) -> #f

Do you have any idea what I should do?

  • 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-15T09:04:41+00:00Added an answer on June 15, 2026 at 9:04 am

    You’re not using recursion anywhere in the code. In fact, it has errors and I don’t think you tested it thoroughly. For example:

    • The if condition should be (null? ls)
    • Using list-ref is not the way to go when traversing a list in Scheme, for that in general you want to use recursion, car, cdr, etc.
    • Again, where is the recursive call? compare should be called at some point!

    I believe this is what you intended, it’s not recursive but it’s the simplest way to implement the procedure:

    (define (compare ls pred?)
      (apply pred? ls))
    

    Because this looks like homework I can only give you some hints for solving the problem from scratch, without using apply. Fill-in the blanks:

    (define (compare ls pred?)
      (if <???>                    ; special case: if the list is empty
          <???>                    ; then return true
          (let loop ((prev <???>)  ; general case, take 1st element
                     (ls   <???>)) ; and take the rest of the list
            (cond (<???>           ; again: if the list is empty
                   <???>)          ; then return true
                  (<???>           ; if pred? is false for `prev` and current element
                   <???>)          ; then return false
                  (else            ; otherwise advance the recursion
                   (loop <???> <???>)))))) ; pass the new `prev` and the rest of the list
    

    Notice that I used a named let for implementing the recursion, so loop is the recursive procedure here: you can see that loop is being called inside loop. Alternatively you could’ve defined a helper procedure. I had to do this for taking into account the special case where the list is initially empty.

    The recursion works like this for the general case: two parameters are required, prev stores the previous element in the list and ls the rest of the list. at each point in the traversal we check to see if the predicates is false for the previous and the current element – if that is the case, then we return false. If not, we continue the recursion with a new prev (the current element) and the rest of the list. We keep going like this until the list is empty and only then we return true.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to loop through a bunch of documents I have to put
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:

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.