foldl and foldr

6 views
Skip to first unread message

Ralf Hemmecke

unread,
Feb 13, 2017, 4:01:22 PM2/13/17
to aldor-devel
Hi Peter,

In 5dae29765f95c0c127034b87ecbd78d8dc77150f you touch the file
lib/aldor/src/datastruc/sal_fold.as.

I would probably have introduced the names foldl and foldr.

Suppose Z==>Integer and that I have a non-associative function f:(Z,Z)->Z.

Without documentation, I don't know whether

f / [1,2,3,4]

is equivalent to

f(1, f(2, f(3, 4)))

or to

f(f(f(1, 2), 3), 4)

Doesn't make that the "/" notation useless?

I can probably live with using \ for the second fold direction, but not
without documentation for either of them.

Ralf

Peter Broadbery

unread,
Feb 13, 2017, 5:29:32 PM2/13/17
to Ralf Hemmecke, aldor-devel
It should match axiom's notation. Will check against it and document- unless you want to take a look?  


Ralf

--
You received this message because you are subscribed to the Google Groups "aldor-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aldor-devel+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ralf Hemmecke

unread,
Feb 14, 2017, 2:28:21 AM2/14/17
to aldor...@googlegroups.com, fricas-devel
Can it be that this /-fold-notation is interpreter-only in FriCAS?

fricas/src/algebra> grep '^[ "_]*/[ "]*:' *.spad
catdef.spad: "/": (%,%) -> %
catdef.spad: "/": (%,%) -> % ++ x/y is the same as x
times the inverse of y.
catdef.spad: "/" : (%, S) -> %
equation1.spad: "/": (%, %) -> %
ffnb.spad: "/" :(VGF,VGF) -> VGF
float.spad: _/ : (%, I) -> %
fraction.spad: _/ : (%, R) -> %
fraction.spad: _/ : (M, R) -> %
fraction.spad: _/ : (%, R) -> %
fraction.spad: _/ : (A, R) -> %
fraction.spad: _/ : (S, S) -> %
fspace.spad: "/" : (MP, MP) -> %
mantepse.spad: _/ : (%, %) -> %
mantepse.spad: _/ : (%, %) -> %
matcat.spad: "/": (%,R) -> %
matcat.spad: "/": (%,R) -> %
mkfunc.spad: "/" : (%, %) -> %
outform.spad: "/": (%, %) -> %
pattern.spad: "/" : (%, %) -> %
sf.spad: _/ : (%, Integer) -> %
sttaylor.spad: "/" : (ST A,ST A) -> ST A
xlpoly.spad: "/" : (%,R) -> %

Ralf

On 02/13/2017 11:29 PM, Peter Broadbery wrote:
> It should match axiom's notation. Will check against it and document- unless you
> want to take a look?
>
> On 13 Feb 2017 21:01, "Ralf Hemmecke" <ra...@hemmecke.org
> <mailto:ra...@hemmecke.org>> wrote:
>
> Hi Peter,
>
> In 5dae29765f95c0c127034b87ecbd78d8dc77150f you touch the file
> lib/aldor/src/datastruc/sal_fold.as
>

Peter Broadbery

unread,
Feb 14, 2017, 2:38:53 AM2/14/17
to Ralf Hemmecke, fricas-devel, aldor...@googlegroups.com
Interesting.  It definitely does work in .spad files (search for ^+\[).  

In any case,  as written it's left to right. There's an argument that it should be undefined - ie require an associative argument (for parallel accumulation) but that is likely to be more work than I have time for at the moment. 

Ralf Hemmecke

unread,
Feb 14, 2017, 3:03:45 AM2/14/17
to fricas...@googlegroups.com, aldor-devel
On 02/14/2017 08:38 AM, Peter Broadbery wrote:
> Interesting. It definitely does work in .spad files (search for ^+\[).

Oh...

https://github.com/fricas/fricas/blob/master/src/algebra/aggcat.spad#L146

count(f : S -> Boolean, c : %) == _+/[1 for x in parts c | f x]

Then it even looks like a compiler builtin in FriCAS, if it appears
already in aggcat.spad.

Maybe FriCAS should also make this into a library feature?

> In any case, as written it's left to right. There's an argument that it should
> be undefined - ie require an associative argument (for parallel accumulation)
> but that is likely to be more work than I have time for at the moment.

Peter, that's not urgent. I just wanted to make some comments that
related to your aldor-patch series.

Ralf

Waldek Hebisch

unread,
Feb 14, 2017, 6:40:19 AM2/14/17
to Ralf Hemmecke, fricas...@googlegroups.com, aldor-devel
>
> On 02/14/2017 08:38 AM, Peter Broadbery wrote:
> > Interesting. It definitely does work in .spad files (search for ^+\[).
>
> Oh...
>
> https://github.com/fricas/fricas/blob/master/src/algebra/aggcat.spad#L146
>
> count(f : S -> Boolean, c : %) == _+/[1 for x in parts c | f x]
>
> Then it even looks like a compiler builtin in FriCAS, if it appears
> already in aggcat.spad.
>
> Maybe FriCAS should also make this into a library feature?
>

ATM this is done via special parsing rule. And compiler can
generate quite efficient code for it. Without much better
support for inlining using library functions would mean
efficiency loss.

--
Waldek Hebisch

Peter Broadbery

unread,
Feb 16, 2017, 4:05:49 PM2/16/17
to fricas-devel, aldor-devel
Thanks - will add a fold and foldr as then '/' can be left as possibly needing an associative argument. 

Will be after the current patch is merged.


Ralf

--
You received this message because you are subscribed to the Google Groups "FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fricas-devel+unsubscribe@googlegroups.com.
To post to this group, send email to fricas...@googlegroups.com.
Visit this group at https://groups.google.com/group/fricas-devel.
Reply all
Reply to author
Forward
0 new messages