1

I have to use DrRacket for this problem but every time I run the code, I get this error message "gcd: this name was defined previously and cannot be re-defined". (also I choose the language as Advanced student [custom] in DrRacket. below is my code, its a recursive function to find the greatest common divisor:

(define (gcd n m)
   (cond [(= m 0) n]
    [else (gcd m (modulo n m))]))

 (check-expect (gcd 0) 0)
 (check-expect (gcd 12 8) 4)
 (check-expect (gcd 6 12 8) 2)
Alex Knauth
  • 8,133
  • 2
  • 16
  • 31
highspeedpog
  • 117
  • 1
  • 1
  • 8

1 Answers1

3

The gcd function is already provided as part of the Advanced Student language, as you can see in the documentation here. Unlike the full Racket language, the teaching languages do not permit defining functions with the same name as library functions. Note that if you remove your definition of gcd, all your tests pass.

If this is homework, then you probably need to name your gcd function something else. If the assignment requires that your function be named gcd, then there’s probably an issue with the assignment.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
  • oh yeah changing the name works but now I'm faced with an error function. I changed it to gcd-structural and got this "gcd-expects 2 arguments, but found only 1: expects 2 arguments, but found only 1". I'm so confused. – highspeedpog Feb 01 '17 at 19:09
  • Actually you know what I found the problem with the check expect, it can't be 0 or something else. only 2 arguments, so the second check-expect is the correct form. well this tells people of how stupid I get sometimes haha... hey thanks for your help though. – highspeedpog Feb 01 '17 at 19:14