The coding of the approximation sequence is recursive. DrRacket diagram

Most recently, a question was asked: "How to find an error in my sequence. DrRacket, Scheme" , and then deleted by an impatient adek.

Since I was in the process of entering the very few last words of my answer, here it was sent reposted, Q and A, so that it would not be in vain.

Here he is quoted almost verbatim:


I am trying to solve this sequence:

enter image description here

I do this in a loop do, a stop-expression condition

enter image description here

But my loop is donot working. I can not find the error. Please help me. My code is:

(define (y a n x )
  (if (= n 0)
  a
  (* 0.5 (+ (y a (- n 1) x) (/ x (y a (- n 1) x))))
  )
  )
(define (f a x e)

 (do
  ( (n 0 (+ n 1)) )  


( ( < (abs (- (sqr (y a n x)) (sqr (y a (- n 1) x)))) e) ("end of loop")) 

(display (y a n x))
(newline)

)
 )
+4
source share
1 answer

Indentation is your friend, not your enemy:

(define (y a n x)
  (if (= n 0)
    a
    (* 0.5 (+      (y a (- n 1) x) 
              (/ x (y a (- n 1) x)) )) ))

(define (f a x e)    
  (do ( (n 0 (+ n 1)) )  
      ( (< (abs (- (sqr (y a    n    x)) 
                   (sqr (y a (- n 1) x)) ))
           e) 
        ("end of loop")) 
    (display (y a n x))
    (newline)))

. Prolog. .

-, n- (n-1) - . , .


, .

Corecursion, .. , .

, n 0 , n ,

0
1 0
2 1 0
3 2 1 0
....

, 0, 1:

0 1 2 3 ....

, ?

, ? ! n.

, (- 0 1) - , y do f, , . , ; ( ), , .

+4

Source: https://habr.com/ru/post/1690740/


All Articles