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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:17:05+00:00 2026-06-13T00:17:05+00:00

I’m trying to write a function which accumulates the value interval with the given

  • 0

I’m trying to write a function which accumulates the value interval with the given operator and adds the initial value. Example:

(accumulate-interval + 0 2 4) :
2 + 3 + 4 + 0 = 9

(accumulate-interval * 1 2 5) :
2 * 3 * 4 * 5 * 1 = 120

Note: Only (+) and (*) to work is enough for me.

My code is:

(define accumulate-interval
  (lambda (op init lower upper) (if (= upper lower)
   (lambda (x) (op x init))
    (lambda (x)
      (op
      ((accumulate-interval op init lower (- upper 1)) x))))     ))

It returns a procedure instead of a value.
I’d really appreciate if you can 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-06-13T00:17:06+00:00Added an answer on June 13, 2026 at 12:17 am

    It returns a function because that is what you are returning. lambda is a function which creates an anonymous function and returns a reference to it. When your code does

    (define accumulate-interval (lambda (op init lower upper) ......))
    

    It is using lambda to create an anonymous function, return the reference to it and assign that reference as the value of accumulate-interval. accumulate-interval is thus associated with that function and that function is run whenever accumulate-interval is evaluated in the function position in a list.

    Now, your accumulate-interval function consists of a single if expression which does

    (if (= upper lower) (lambda (x) ...) (lambda (x) ...))
    

    So if upper and lower are equal, it returns a reference to one anonymous function, otherwise it returns a different one. This is what you have told it to do.

    I don’t know what you are trying to do, here, but I suspect you just want to execute the code which you are mistakenly wrapping in a function and returning. So I think your code is meant to look more like

    (if (= upper lower)
      (op upper init) (op lower (accumulate-interval op init (+ lower 1) upper)))
    

    Which should work, although I think you need to rethink your arguments because that recursive call is not in tail position.

    Thinking about it,

    (if (= upper lower)
       (op upper init)
       (accumulate-interval (op upper init) lower (- upper 1)))
    

    Would work and be tail-recursive.

    Note: I don’t normally do people’s homework for them, but I think you may have more than one misconception about Scheme syntax and showing working code was easier than exploring each of those possible misunderstandings separately and in combination.

    • 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’Everest What PHP function
I'm trying to select an H1 element which is the second-child in its group
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
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 used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.