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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:18:43+00:00 2026-06-11T21:18:43+00:00

We have this code (define (checksum-2 ls) (let ([x (reverse ls)]) (cond [(null? ls)

  • 0

We have this code

(define (checksum-2 ls)
  (let ([x (reverse ls)])
    (cond
      [(null? ls) 0]
      [else (+ (*  (length ls) (car x)) (checksum-2 (cdr x)))])))

It reverses this list

'(4 6 7 5 6)

It’s suppose to return 87 but it returns 80. Can anyone help us debug this?

  • 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-11T21:18:44+00:00Added an answer on June 11, 2026 at 9:18 pm

    As written, this function will return

    5 * 6 + 4 * 4 + 3 * 5 + 2 * 6 + 1 * 7 = 80
    

    This is because at each stage, it takes the first element of the reversed list (i.e. the last element of the list) and multiplies it by the list length, and adds it to the result of calling checksum-2 on the reversed list. The list is then reverse again, so the next element to be added is the one that was originally at the front of the list (which is 4 in this case).

    What you want to do is reverse the list once, and then work with the reversed list from then on. To do this you can use a helper function:

    (define (chksum ls)
      (chksum-helper (reverse ls)))
    
    (define (chksum-helper ls)
      (cond
        ((null? ls) 0)
        (else (+ (* (length ls) (car ls))
                 (chksum-helper (cdr ls))))))
    

    Now you only have to reverse the list once, which is a big win in efficiency. To clean up the code you can factor the definition of chksum-helper inside the definition of chksum, getting something like this:

    (define (chksum ls)
      (define (chksum-helper x)
        (cond
          ((null? x) 0)
          (else (+ (* (length x) (car x))
                   (chksum-helper (cdr x))))))
      (chksum-helper (reverse ls)))
    

    I renamed the argument to chksum-helper from ls to x to prevent confusion, but you don’t actually have to do this – it would work just as well if you left it as ls. This is about as compact as you’re going to get it.

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

Sidebar

Related Questions

I have this code in linux kernel: #define task_cred_xxx(task, xxx) ({ __typeof__(((struct cred *)NULL)->xxx)
I have this (working) CPU code: #define NF 3 int ND; typedef double (*POT)(double
I have some Objective-C code that looks like this: #define myVar 10 float f
I have this code: #include <stdio.h> #include <math.h> #define gridSize 400 void main() {
I have this code : #define N 100 //starting size of the array int
I have this code: #define _WIN32_WINNT 0x0500 #include <cstdlib> #include <iostream> #include <windows.h> using
I have this code: #define _WIN32_WINNT 0x0500 #include <cstdlib> #include <iostream> #include <windows.h> using
I have this code: (define-foreign-library libc (:unix libc.so.6)) (use-foreign-library libc) (defcfun setlocale :pointer (category
I have this macro from someone's code: #define Q_DEF_PROTOTYPE( Type, Name ) Type (*Name)
I have this code for example. #include <stdlib.h> #include <stdio.h> #define array_size 3 typedef

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.