(defsuite "named-let-set"
  (deftest "set! counter in named let loop"
    (assert-equal 5
      (let ((x 0)) (let loop () (when (< x 5) (set! x (+ x 1)) (loop))) x)))

  (deftest "set! list accumulator in named let with param"
    (assert-equal (list 0 1 2)
      (let ((acc (list)))
        (let loop ((i 0))
          (when (< i 3) (set! acc (append acc (list i))) (loop (+ i 1))))
        acc)))

  (deftest "append! + set! counter in named let"
    (assert-equal (list 0 1 2)
      (let ((acc (list)) (i 0))
        (let loop () (when (< i 3) (append! acc i) (set! i (+ i 1)) (loop)))
        acc)))

  (deftest "set! sum in named let loop (1..10)"
    (assert-equal 55
      (let ((sum 0))
        (let loop ((i 1))
          (when (<= i 10) (set! sum (+ sum i)) (loop (+ i 1))))
        sum)))

  (deftest "set! string accumulator in named let"
    (assert-equal "012"
      (let ((result ""))
        (let loop ((i 0))
          (when (< i 3) (set! result (str result (str i))) (loop (+ i 1))))
        result)))

  (deftest "set! in plain let (baseline)"
    (assert-equal 42 (let ((x 0)) (set! x 42) x)))

  (deftest "named let without set! (baseline)"
    (assert-equal 10
      (let loop ((i 0) (acc 0)) (if (< i 5) (loop (+ i 1) (+ acc i)) acc)))))
