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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:55:08+00:00 2026-06-17T06:55:08+00:00

(define wadd (lambda (i L) (if (null? L) 0 (+ i (car L))) (set!

  • 0
(define wadd (lambda (i L)
                (if (null? L) 0
                    (+ i (car L)))
                    (set! i (+ i (car L)))
                          (set! L (cdr L))))

(wadd 9 '(1 2 3))

This returns nothing. I expect it to do (3 + (2 + (9 + 1)), which should equate to 15. Am I using set! the wrong way? Can I not call set! within an if condition?

  • 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-17T06:55:10+00:00Added an answer on June 17, 2026 at 6:55 am

    I infer from your code that you intended to somehow traverse the list, but there’s nothing in the wadd procedure that iterates over the list – no recursive call, no looping instruction, nothing: just a misused conditional and a couple of set!s that only get executed once. I won’t try to fix the procedure in the question, is beyond repair – I’d rather show you the correct way to solve the problem. You want something along these lines:

    (define wadd
      (lambda (i L)
        (let loop ((L L)
                   (acc i))
          (if (null? L)
              acc
              (loop (cdr L) (+ (car L) acc))))))
    

    When executed, the previous procedure will evaluate this expression: (wadd 9 '(1 2 3)) like this:
    (+ 3 (+ 2 (+ 1 9))). Notice that, as pointed by @Maxwell, the above operation can be expressed more concisely using foldl:

    (define wadd
      (lambda (i L)
        (foldl + i L)))
    

    As a general rule, in Scheme you won’t use assignments (the set! instruction) as frequently as you would in an imperative, C-like language – a functional-programming style is preferred, which relies heavily on recursion and operations that don’t mutate state.

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

Sidebar

Related Questions

(define sp (lambda (l) (if (null? l) '(0 1) (let ((v (sp (cdr l))))
(define cart-product (lambda (sos1 sos2) (if (null? sos1) '() (cons (cart-prod-sexpr (car sos1) sos2)
(define length1 (lambda (lat) (cond ((null? lat) 0) (else (+ 1 (length1 (cdr lat)))))))
(define min (lambda (l m c) (cond ((null? l) (print m)) (else ((c (car
(define (lcs lst1 lst2) (define (except-last-pair list) (if (pair? (cdr list)) (cons (car list)
#define SIZE 9 int number=5; char letters[SIZE]; /* this wont be null-terminated */ ...
(define test (lambda args (if (= (length args) 1) (display (car args)) (begin (display
(define self-add (let ((x 0)) (lambda () (set! x (+ x 1)) x))) (self-add)
I define an enums using include,since there are different enums which have the same
I define a tabs using JQuery <div id=tabs> <ul> <li><a href=#tabs-1>Tab 1</a></li> <li><a href=#tabs-2>Tab

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.