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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:56:16+00:00 2026-05-25T06:56:16+00:00

I am learning Scheme, and I’ve read the basics but I still can’t figure

  • 0

I am learning Scheme, and I’ve read the basics but I still can’t figure how to “map” a Java class to Scheme code. Could any of you guys help me out here? I just need someone to show me how this looks in Scheme to grasp the final details and get things going in my head:

public class sumFibonacciValues {
    public static void main(String [] args) {
        int n = 4000000;
        long i2 = 1, i1 = 1, Fibo = 0, temp = 1;
        while(i2 < n) {
            temp = i1 + i2;
            i1 = i2;
            i2 = temp;
            if(i2 % 2 == 0)
                Fibo += i2;
        }
        System.out.println(Fibo);
    }
}
  • 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-25T06:56:16+00:00Added an answer on May 25, 2026 at 6:56 am

    I wouldn’t have answered something that looks so much like homework, but the “idiomatic” comment just begged for a demonstration that it’s really not that far. First, a direct translation:

    (define (sum-fibonacci-values)
      (define n 4000000)
      (define i2 1)
      (define i1 1)
      (define fibo 0)
      (define temp 1)
      (let loop ()
        (when (< i2 n)
          (set! temp (+ i1 i2))
          (set! i1 i2)
          (set! i2 temp)
          (when (zero? (modulo i2 2)) (set! fibo (+ fibo i2)))
          (loop)))
      (write fibo))
    

    Second, make it “idiomatic”, by removing the redundant mutations, and instead just bind new values, and using a tail-recursive loop. Note that this code is still in direct correlation with the original:

    (define (sum-fibonacci-values)
      (define n 4000000)
      (let loop ([i2 1] [i1 1] [fibo 0] [temp 1])
        (if (< i2 n)
          (let* ([temp (+ i1 i2)]
                 [i1 i2]
                 [i2 temp]
                 [fibo (if (zero? (modulo i2 2)) (+ fibo i2) fibo)])
            (loop i2 i1 fibo temp))
          fibo)))
    

    Finally, now that the code is clearer, you can see that there are some redundancies. Here’s a cleaned up version:

    (define (sum-fibonacci-values)
      (define n 4000000)
      (let loop ([i2 1] [i1 1] [fibo 0])
        (if (< i2 n)
          (let ([i3 (+ i1 i2)])
            (loop i3 i2 (if (zero? (modulo i3 2)) (+ fibo i3) fibo)))
          fibo)))
    

    Note that the same cleanup can be done on the Java code. (But that’s really left as an exercise to the reader…)

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

Sidebar

Related Questions

I've just started learning Functional Programming (Scheme). But I still have problems thinking functionally.
Okay so I have started a new language in class. We are learning Scheme
I'm learning PLT Scheme and I want to know how can I build a
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
So... I'm new to scheme r6rs, and am learning macros. Can somebody explain to
I am just learning scheme, but I would love to be able to repeat
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (<
I'm just started learning scheme and can't quite understand why this function does not
I am learning Scheme. What is wrong with the code below?I want to write
I'm learning Scheme in a class and my professor doesn't answer questions after 8:00,

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.