読者です 読者をやめる 読者になる 読者になる

kiyo_hikoのブログ

メモ+日記?

VBAでForとFor Eachを学んだ

VBA/VBS/UWSC

VBAでiotaを書いた。

Function iota(count As Long, _
              Optional start As Long = 0, _
              Optional step As Long = 1)
    Dim i As Long
    Dim n As Long
    Dim ns As Collection
    i = 0
    n = start
    Set ns = New Collection
    For i = 0 To count - 1
        ns.Add n
        n = n + step
    Next i
    Set iota = ns
End Function

Sub Run()
    Dim n As Variant
    Dim ns As Collection
    Set ns = iota(100, 1, 5)
    For Each n In ns
        Debug.Print n
    Next n
End Sub

別件としてBLOCKとTAGBODYでiotaを書いていた。

(defun iota (count &optional (start 0) (step 1))
  (let ((i count) (n start) (ns nil))
        (block nil
          (tagbody
           next
           (if (<= i 0) (return (nreverse ns)))
           (push n ns)
           (incf n step)
           (decf i)
           (go next)))))