Scheme / Racket: do an assessment cycle

In both r6rs and Racket circuits, the following procedure applies:

;; create a list of all the numbers from 1 to n
(define (make-nums n)
  (do [(x n (- x 1)) (lst (list) (cons x lst))]
    ((= x 0)
     lst)))

I tested it for both r6rs and Racket, and it works fine, but I know for sure for DrRacket.

My question is what is guaranteed , that in this case will be calculated step of ( (- x 1)and (cons x lst)). If this is not guaranteed, then my procedure is not very stable.

I did not see anything that would indicate this in the standards for any language, but I ask here because when I tested it, it was evacuated in order.

+3
source share
1 answer

, , . , : x lst, , , , .

, :

(define (make-nums n)
  (do ([x n (- x 1)] [lst null (cons x lst)])
      [(zero? x) lst]))

named- let:

(define (make-nums n)
  (let loop ([x n] [lst null])
    (if (zero? x)
      lst
      (loop (- x 1) (cons x lst)))))

( - let):

(define (make-nums n)
  (define (loop x lst)
    (if (zero? x)
      lst
      (loop (- x 1) (cons x lst))))
  (loop n null))

, loop .

, , Racket . , . , , , , . , :

(list (read-line) (read-line))

Racket , . .

+7

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


All Articles