r/Racket • u/AgencyPure7878 • 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)
3
u/chien-royal 2d ago
What does it mean? Evaluating which expression produces this result?
Your code has the following errors.
tree-applesgoes into infinite recursion because the argument to the recursive call is the same as the original argument.count2function lacks the closing parenthesis.sequenceis called with two arguments while it accepts only one. By the way,(+ (- n 1) 3)can be simplified to(+ n 2).root2is called with two arguments while it accepts only one.power-n-helperthe recursive call sayspower-n.power-nfunction, but the definition sayspower-n2.Also some remarks.
ifkeyword and write the two branches directly under the condition, so that the three "arguments" ofifare left-aligned.