Syntax error with Sum of Multiples

When I try to test my code, it seems to run for a few seconds, then says there’s a syntax error.

Manually running the test cases locally, in either MIT Scheme(used for SICP) and Guile 3(Debian, though, so it might be a few minor versions behind what’s here) gives the correct answer for any of them, no errors or warnings of any kind.

Any tips on what to look for? I did copy/paste the code from emacs to here, not sure if that might have introduced some whitespace weirdness or something though it hasn’t on other exercises.

It just says syntax error, nothing to point to where(it may be in the test files or the include exercism puts in for all I know)

Can you provide your code as text (not as an image)?

It’s a bit more code than it seems most people used(if I don’t get a solution here I’ll be digging more into the community solutions) but here it is.

(import (rnrs))

(define (get-multiples-to base limit)
  (define (iter cur)
    (if (>= (* cur base) limit)
	'()
	(cons (* cur base) (iter (+ cur 1)))
	))
  (iter 1))

(define (get-multiples-lists fs limit)
  (if (null? fs)
      '()
      (cons (get-multiples-to (car fs) limit)
	        (get-multiples-lists (cdr fs) limit))))

(define (in-list? x xs)
  (cond ((null? xs) #f)
	    ((= x (car xs)) #t)
	    (else (in-list? x (cdr xs)))))

(define (list-union xs ys)
  (cond ((null? xs) ys)
	    ((in-list? (car xs) ys) (list-union (cdr xs) ys))
	    (else (append (list (car xs)) (list-union (cdr xs) ys)))))

(define (union-of-multiple-lists xs limit)
  (define lists (get-multiples-lists xs limit))
  (define (iter xs)
    (if (null? (cdr xs))
	    (car xs)
	    (list-union (car xs) (iter (cdr xs)))))
  (iter lists))

(define (sum-of-multiples ints limit)
  (sum (union-of-multiple-lists ints limit)))
  

@anniethebruce I see that the very last line of your code is using an undefined function sum. You can replace the line with

(apply + (union-of-multiple-lists ints limit)))

for a quick fix. But I suspect that your code is failing to terminate correctly, even after this fix.

1 Like

With that change exercism at least tries to run it. Im not home so i cant check, but the original source is a big scratchpad sorta file and im guessing i wrote a sum function there, then forgot it was not a built in. Which explains why it ran fine locally.

It times out, but i was getting correct answers locally so its an optimization issue at this point(or a badly handled edge case causing an infinite loop) I’ll probably be back if that ends up too hard.

1 Like