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

Re: Folds and reductions

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



        Now that I think about it, there are a couple of small improvements 
to be made in the code I wrote at
http://srfi.schemers.org/srfi-1/mail-archive/msg00021.html:

        In PAIR-FOLDL, it's pointless to cons ANS onto LISTS in the call
(APPLY F (CONS ANS LISTS)), since if consing is needed APPLY will do it
internally:

(define (pair-foldl f zero lis1 . lists)
  (if (pair? lists)
      (let lp ((lists (cons lis1 lists)) (ans zero))
        (if (%all-pairs? lists)
            (let ((tails (%cdrs lists)))
              (lp tails (apply f ans lists)))
            ans))
      (let lp ((lis lis1) (ans zero))
        (if (pair? lis)
            (let ((tail (cdr lis)))
              (lp tail (f ans lis)))
            ans))))

        Given my remarks in (1) and (2) of the earlier message, LIDENTITY
would be a better identifier for the third parameter of REDUCEL:

(define (reducel f lidentity lis)
  (if (pair? lis)
      (foldl f (car lis) (cdr lis))
      lidentity))

-- 
======  John David Stone - Lecturer in Computer Science and Philosophy  =====
==============  Manager of the Mathematics Local-Area Network  ==============
==============  Grinnell College - Grinnell, Iowa 50112 - USA  ==============
==========  stone@xxxxxxxxxxxxx - http://www.math.grin.edu/~stone/  =========