drym-org / qi

An embeddable flow-oriented language.
59 stars 12 forks source link

Add a `loop` form #1

Open countvajhula opened 3 years ago

countvajhula commented 3 years ago

Add a loop form, which would compile to a lambda expression defined in terms of itself, and would be specified in terms of a set of functions, something like:

(define result (loop ('(1 2 3)) (until empty?) (map first) (combine +) (next rest))

... along with the lambda forms:

(define sum-nums (loop-lambda (nums) (until empty?) (map first) (combine +) (next rest))

and

(define-loop (sum-nums nums) (until empty?) (map first) (combine +) (next rest))

Of course, these examples may be better expressed simply using folds or the built-in for forms. But there may be other cases where this approach is preferable. For instance, it may be that these would be preferable to named lets and handwritten recursive functions in some cases. The idea is simply to provide forms to define loops exclusively in terms of functions with the arguments already implicit (much like the other forms provided by this package), and see where that gets us.