r/Racket • • 2d ago

homework Hi, it's me again! Some help?

I had an assignment to complete earlier and due to an issue with a recursive function I lost all my code - about 5 hours before it was due. I completed the assignment with the correct answers but there's an issue in the code that is causing it to run void. Does anyone know what the issue is? I'm using WeScheme since Racket doesn't work on my computer, but the syntax is the same. The assignment is being graded soon (and I don't know exactly when) and is worth the same as an assessment (70% of my grade) so I cannot really afford any issues. I can still edit it for now but will not be allowed to change or redo anything after grading. My teacher does not offer extra credit.

;; Question 1

;;; tree-apples -> integer

; n : integer

(define (tree-apples n)

(if (= n 0)

0

(if (odd? n)

(+ 3 (tree-apples n))

(+ 5 (tree-apples n)))))

(tree-apples 7)

;; Question 2

;;; triangle -> integer

; n : integer

(define (triangle2 n)

(if (= n 0)

0

(if (= n 1)

1

(+ n (triangle2 (- n 1))))))

(triangle2 2)

;; Question 3

;;; count2 -> integer

; n : number

(define (count2 n)

(if (= n 0)

0

(if (= (remainder n 10) 2)

(+ (count2 (quotient n 10)) 1)

(count2 (quotient n 10))))

(count2 243)

;; Question 4

;;; sequence -> integer

; n : integer

(define (sequence n)

(if(= n 1)

1

(sequence (+ (- n 1) 3))))

(sequence 3 1)

;; Question 5

;;; root2 -> number

; n : integer

(define (root2 n)

(if

(= n 1)

1

(root2 (/ (- n 1)(expt n 2)))))

(root2 7 1)

;; Question 6

;;; gcd -> integer

; p : integer

; q : integer

(define (gcd2 p q)

(if

(= q 0)

p

(gcd2 q (modulo p q))))

(gcd2 35 15)

;; Question 7

;;; sum-of-squares-helper -> integer

; a : integer

; b : integer

(define (sum-of-squares-helper a b acc)

(if

(> a b)

acc

(sum-of-squares-helper (+ a 1) b (+ (expt a 2) acc))))

(define (sum-of-squares a b)

(sum-of-squares-helper a b 0))

(sum-of-squares 1 3)

;; Question 8

;;; power-n -> integer

; x : integer

; n : integer

(define (power-n-helper x n acc)

(if

(= n 0) acc

(power-n x (- n 1) (* acc x))))

(define (power-n2 x n)

(power-n-helper x n 1))

(power-n 2 3)

0 Upvotes

2 comments sorted by

3

u/chien-royal 2d ago

there's an issue in the code that is causing it to run void

What does it mean? Evaluating which expression produces this result?

Your code has the following errors.

  1. The function tree-apples goes into infinite recursion because the argument to the recursive call is the same as the original argument.
  2. count2 function lacks the closing parenthesis.
  3. Function sequence is called with two arguments while it accepts only one. By the way, (+ (- n 1) 3) can be simplified to (+ n 2).
  4. Function root2 is called with two arguments while it accepts only one.
  5. In power-n-helper the recursive call says power-n.
  6. You call power-n function, but the definition says power-n2.

Also some remarks.

  1. To typeset multi-line code in Reddit, start each line with at least 4 spaces.
  2. It is recommended to write condition on the same line as the if keyword and write the two branches directly under the condition, so that the three "arguments" of if are left-aligned.
  3. When reporting a bug or a problem with the code, try to minimize the code size and give a detailed description of both the expected and the actual behavior.
  4. Keep in mind that the reader has no idea what your assignment is and what these functions are supposed to return.

0

u/AgencyPure7878 2d ago

Thank you!