[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LAST CALL for SRFI-113, Sets and bags

This page is part of the web mail archives of SRFI 113 from before July 7th, 2015. The new archives for SRFI 113 contain all messages, not just those from before July 7th, 2015.



Nice! You're right that fold-right is more expensive than fold, but I didn't think it'd matter much since the argument list is expected to be pretty short. Anyway, nice to have this figured out.

Best regards,
Kevin Wortman


On Fri, Aug 22, 2014 at 1:26 PM, John Cowan <cowan@xxxxxxxxxxxxxxxx> wrote:
Kevin Wortman scripsit:

> (define (cartesian . sets)
>   (fold-right (lambda (set tuples)
>         (append-map! (lambda (element)
>                    (map (cute cons element <>) tuples))
>                  set))
>           '(())
>           sets))

This is the same algo that I worked out walking to work today, except
that I reversed "sets" in advance (it's safe to use reverse!) and did
a left fold, because left folds are tail-recursive in eager languages.

--
John Cowan          http://www.ccil.org/~cowan        cowan@xxxxxxxx
You know, you haven't stopped talking since I came here. You must
have been vaccinated with a phonograph needle.
        --Rufus T. Firefly