This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Need help with a function in DrScheme

2 posts · 4121 views

I can write the recursion version correctly need help with writing a do loop in DrScheme.

After the do loop the variable x is sent to car L intially and then is updated to first element of everything but first element in list.
(Toggle Plain Text)
(define (sumiteration L)
  (do ((x (car L) (car(cdr L))))
    ((null? L))
    (set! sum (+ sum x)))
)

Re: Need help with a function in DrScheme

The direct answer might be:
(define (sumiteration someList)
  (do ((sum 0)
       (L someList (cdr L)))
    ((null? L) sum)
    (set! sum (+ sum (car L)))))
1) There was a sum referenced in the (set! sum (+ sum x)) that was not previously defined.
2) Nothing advanced the list L.

For a reasonably short input list, you might:

(apply + your-list-of-numbers)

It may also be of interest to use srfi/1 reduce which won't have a problem with large lists.

(require srfi/1)
(reduce + 0 your-list-of-numbers)