slib-discuss
[Top][All Lists]
Advanced

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

[Slib-discuss] SRFI-1's fold-left, pair-fold-left & reduce-left


From: Peter Kourzanov
Subject: [Slib-discuss] SRFI-1's fold-left, pair-fold-left & reduce-left
Date: Wed, 31 Oct 2012 17:24:48 +0100

Hi,

Here's a patch to srfi-1.scm in latest SLIB to rectify some of the
shortcomings of SRFI-1:

1. FOLD does not fold left, as seen in Haskell & OCaml
   (flipped arguments). This patch includes a non-flipped FOLD-LEFT 
   (which also doesn't need an APPEND!, BTW).

2. REDUCE-LEFT in term of FOLD-LEFT

3. PAIR-FOLD of SRFI-1 does not fold left on the input list. In fact,
   current implementation produces the result of PAIR-FOLD-RIGHT in 
   reverse:

(pair-fold [lambda (x y) (cons (reduce + 0 x) y)] '() '(1 2 3))
->
(3 5 6)

(pair-fold-right [lambda (x y) (cons (reduce + 0 x) y)] '() '(1 2 3))
->
(6 5 3)

   This patch adds an optimized implementation of actual PAIR-FOLD-LEFT,
   with non-flipped arguments.

(pair-fold-left [lambda (y x) (cons (reduce + 0 x) y)] '() '(1 2 3))
->
(6 3 1)

(pair-fold-left [lambda (y x) (append y (list (reduce + 0 x)))] '() '(1
2 3))
->
(1 3 6)

4. FOLD, PAIR-FOLD and REDUCE left for compatibility

Kind regards,

Pjotr Kourzanov

N.B. I haven't tested this code with SCM, only with my own (compiled)   
     version of SLIB for Bigloo.

Attachment: slib-srfi1.diff
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]