Repl.it在线IDE中Scheme代码modulo未绑定符号错误咨询
Great question! This boils down to differences between Scheme implementations and their supported standards.
Here's the scoop: Scheme has several official standards (like R5RS, R6RS, R7RS) plus popular implementations that extend these standards (like Racket, Guile). Your code works on jdoodle because it's likely using a Scheme environment that supports newer standards (R6RS+) or a flexible implementation like Racket—both of which include modulo as a built-in function.
But Repl.it's default Scheme environment sticks closely to the older R5RS standard (the 1998 Revised^5 Report on Scheme), which does NOT include modulo as a core primitive. Instead, R5RS only specifies remainder as the standard integer division remainder function. That's why Repl.it throws the "unbound symbol" error—it doesn't recognize modulo out of the box.
You've got a few solid options to get your GCD code working without leaving Repl.it:
1. Replace modulo with R5RS-standard remainder
For positive integers (the most common use case for GCD), remainder and modulo produce identical results. Just swap out the function call, and your code will run perfectly:
(define (gcd a b) (cond [(= b 0) a] [else (gcd b (remainder a b))] ) )
2. Implement your own modulo function (for negative number support)
If you need to handle negative integers (where remainder and modulo behave differently), you can build your own modulo using R5RS's remainder as a base. Add this definition before your GCD function, and your original code will work as-is:
(define (modulo a b) (let ((r (remainder a b))) (if (or (= r 0) (= (sign r) (sign b))) r (+ r b)))) (define (gcd a b) (cond [(= b 0) a] [else (gcd b (modulo a b))] ) )
3. Switch to Racket on Repl.it
Repl.it lets you change your environment to Racket (a Scheme-derived language with excellent standard support). Racket includes modulo by default, so your original code will run without any edits. To do this:
- Open your Repl.it project
- Click the "Environment" dropdown at the top of the editor
- Select "Racket" from the list
- Restart the environment, then run your code
内容的提问来源于stack exchange,提问作者iamshnoo

