[plt-scheme] Doubt in folding
Two changes:
#lang scheme
;; my-fold-left : (X X -> X) X (listof X) -> (listof X)
;; applies operation to each element of lox, and
;; combines them in a new list. Works from left to right
;; in lox.
(define (my-fold-left combinator initial lox)
(cond
[(empty? lox) initial]
[else
(combinator (first lox) (my-fold-left combinator initial (rest
lox)))]))
;; plt's built in foldl
(foldr cons empty (list 1 2 3 4)) ; evals to what I expected ->
(4 3 2 1)
;; mine
(my-fold-left cons empty (list 1 2 3 4)) ; evals to ((((() . 4) .
3) . 2) . 1), but why ?
;; ----
Hint: Foldl is an accumulator function.
On Oct 22, 2009, at 2:25 PM, Eduardo Bellani wrote:
> Small doubt in a fuction I've been playing with:
>
> ;; my-fold-left : (X X -> X) X (listof X) -> (listof X)
> ;; applies operation to each element of lox, and
> ;; combines them in a new list. Works from left to right
> ;; in lox.
> (define (my-fold-left combinator initial lox)
> (cond
> [(empty? lox) initial]
> [else
> (combinator (my-fold-left combinator
> initial
> (rest lox))
> (first lox))]))
>
> ;; plt's built in foldl
> (foldl cons empty (list 1 2 3 4)) ; evals to what I expected -
> > (4 3 2 1)
>
> ;; mine
> (my-fold-left cons empty (list 1 2 3 4)) ; evals to ((((() . 4) . 3)
> . 2) . 1), but why ?
>
>
> Thanks for the time.
> --
> Eduardo Bellani
>
> www.cnxs.com.br
>
> I object to doing things that computers can do.
> - Olin Shivers
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme