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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:45:50+00:00 2026-06-15T10:45:50+00:00

I’m learning accumulation and foldr but something in my code is wrong. I’d like

  • 0

I’m learning accumulation and foldr but something in my code is wrong. I’d like compare elements in all the list but foldr use only first and second element. Here is my code:

(define accum?
  (lambda (list1 pre?)
  (foldr (lambda (x y) 
               (if (pre? (car list1) (cadr list1)) #t #f))
         #f
         list1)))

(accum? '(1 2 3 4) <) --> #t
(accum? '(3 2 3 4) <) --> #f
(accum? '(1 2 5 4) <) --> #t (should be #f)
(accum? '(5 7 2 3) <) --> #t (should be #f)

Do you have any idea where is a mistake? By the way, it is better to use only (pre? (car list1) (cadr list1)) without if –> (if <…> #t #f)?

  • 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-15T10:45:51+00:00Added an answer on June 15, 2026 at 10:45 am

    This problem can be solved a lot easier with apply, using the fact that the comparison operators receive a variable number of arguments:

    (apply < '(1 2 3 4))
    => #t
    
    (apply < '(1 2 5 4))
    => #f
    

    UPDATE

    For this problem in particular, foldr is not the right tool for the job (although it’s not impossible, as you’ve demonstrated) – there isn’t a value to be “accumulated” while the list is being traversed, the list is either ordered or not for the given predicate.

    From the comments, I understand that the pre? procedure can be an arbitrary lambda that receives exactly two parameters.

    A possible solution would be to create al list of all pairs of consecutive elements, and check if the predicate holds true for all of them. Notice that the last element won’t have a corresponding pair, so we need to exclude it from the list of pairs. For that, I’ll define a special zip procedure that, given two input lists, takes care of creating a list of pairs taking into account the fact that the lists can be of different lengths:

    (define (zip lst1 lst2)
      (if (or (null? lst1) (null? lst2))
          '()
          (cons (list (car lst1) (car lst2))
                (zip (cdr lst1) (cdr lst2)))))
    
    (zip '(1 2 3) '(2 3))
    => '((1 2) (2 3))
    

    Now putting all together, we can solve the original problem like this:

    (define (accum? lst pre?)
      (andmap (lambda (tuple)
                (pre? (car tuple) (cadr tuple)))
              (zip lst (cdr lst))))
    

    Bear in mind that it won’t work for empty lists, but it’s trivial to add a special case for this. Now the comparison can be made with arbitrary lambdas:

    (accum? '(1 2 3) (lambda (a b) (= a (- b 1))))
    => #t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.