[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Announcing the first actually stable release of guile-for-loops
From: |
Zelphir Kaltstahl |
Subject: |
Re: Announcing the first actually stable release of guile-for-loops |
Date: |
Fri, 24 Jan 2020 00:22:33 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 |
Hi Linus!
Although I just ported multiple usages of those kind of loops from a
previously Racket project to a Guile project, I think this is quite
cool! When I ported those usages in my code, it also resulted in some
named lets (I guess quite naturally, as an available looping construct),
so I can relate to that.
Thanks!
Regards,
Zelphir
On 1/23/20 6:00 PM, address@hidden wrote:
> Message: 4
> Date: Thu, 23 Jan 2020 13:10:46 +0100
> From: Linus Björnstam <address@hidden>
> To: address@hidden
> Subject: Announcing the first actually stable release of
> guile-for-loops
> Message-ID: <address@hidden>
> Content-Type: text/plain;charset=utf-8
>
> Hiya everybody!
>
> I have spent some time implementing efficient for loops for guile, and they
> are baked and ready to go. I have worked the last weeks at implementing
> generalized support for non-tail-recursive loops and am happy to announce
> for/foldr. It is a generic right fold, with support for delaying it's
> arguments as either thunks or promises.
>
> The syntax is more or less the same as racket's loops, and they are generally
> compatible. The code generated is for almost all cases as fast as hand-rolled
> code. They are all expressed as left or right folds, and are as such (apart
> from for/list, but read about that in the documentation) free of mutation.
> They are all converted to named lets.
>
> Some examples:
>
> (for/list ((a (in-range 1 6)))
> (* a a)) ;; => (1 4 9 16 25)
>
> (for*/list ((a (in-string "ab")) (b (in-range 1 3)))
> (list a b))
> ;; => ((#\a 1) (#\a 2) (#\b 1) (#\b 2))
>
> There are many more looping constructs, among others:
> for/sum, for/vector, for/or, for/and, for/first, for/last and a
> side-effecting simple for.
>
> Here is a sieve of erathostenes:
>
> (define (erathostenes n)
> (define vec (make-vector n #t))
> (for/list ([i (in-range 2 n)] #:when (vector-ref vec i))
> (for ([j (in-range/incr (* 2 i) n i)])
> (vector-set! vec j #f))
> i))
>
> The code and documentation is available here:
> https://hg.sr.ht/~bjoli/guile-for-loops
>
> A web-friendly documentation can be found here:
> https://man.sr.ht/%7Ebjoli/for-loops-docs/for-loops.md
>
> The thing I had been waiting for is right fold. That allows us to write loops
> like guile's map: non-tail recursive:
> (for/foldr ((identity '())) ((a (in-list '(1 2 3))))
> (cons (* a a) identity))
>
> becomes equivalent to:
>
> (let loop ((random-identifier '(1 2 3)))
> (if (null? random-identifier)
> '()
> (let ((a (car random-identifier)))
> (cons (* a a) (loop (cdr random-identifier))))))
>
> Happy hacking
> Linus Björnstam