# Re: Aggregation (with GBY) is Relational Division

From: vc <boston103_at_hotmail.com>
Date: 6 Jun 2006 08:58:46 -0700

Frank Hamersley wrote:
> David Cressey wrote:
> > "Marshall" <marshall.spight_at_gmail.com> wrote in message
> >> David Cressey wrote:
> >>> "Marshall" <marshall.spight_at_gmail.com> wrote in message
> [..]
> >>> PS: can you point me to a web site that will explain "fold" to me?
> >> I did a bit of searching and came up with a short intro:
> >>
> [..]
> >> Fold is simply inserting a binary operator "in between" the elements
> >> in a collection. Usually an ordered, collection, alas. Since it's an
> >> ordered collection, they have to do the extra work to take order
> >> into account, so you have both foldl and foldr, depending on
> >> whether you start at the "left" end of the list or the "right".
> >> (Beginning vs. end.)
> [..]
> >> foldl(append, [], [1, 2, 3])
> >>
> >> returns [3, 2, 1]

Assuming append is defined:

append [] e = [e]
append list e = reverse (e:reverse list)

i.e. append [1,2,3] 4 would give [1,2,3,4]

fold(append, [], [1,2,3]) would not change anything and produce [1,2,3]

[...]

> I know its just an intro web page ... but does anyone know how foldr
> disperses the start point?

```>
```

> eg> fold1 (+) 4 [1..3] produces a computational sequence 4+1+2+3
>
No, foldl (+) 4 [1..3] => ((4+1)+2)+3 = 10

> so does

```>
```

> eg> foldr (+) 4 [1..3] produce 4+3+2+1 or 3+2+1+4?
>

> ie. is the start point element relocated (or not) or would you write it
> as "foldr (+) [1..3] 4"?

Nothing is relocated, the list is traversed as is, the expression is 'built' differently. Consider

sumr [] = 0
sumr (x:xs) = x + sumr xs

suml [] = 0
suml (x:xs) = suml xs + x

Then

foldr (+) 0 L = sumr L
foldl (+) 0 L = suml L

>
> Cheers, Frank.
Received on Tue Jun 06 2006 - 17:58:46 CEST

Original text of this message