Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foldr vs foldl as reduce operator in APL

Tags:

apl

In Kenneth Iverson’s A Programming Language the reduction operation op/ is defined as a foldl (left fold):

The β—Œ-reduction of a vector π‘₯ is denoted by β—Œ/π‘₯ and defined as
    zβ†β—Œ/π‘₯ ⟺ z = (β‹―((π‘₯₁ β—Œ π‘₯β‚‚) β—Œ π‘₯₃) β—Œ β‹― π‘₯α΅’)

However in modern APLs, it is clearly a right fold (foldr)

      {⍺⍡}/'abcd'
β”Œβ†’β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚a β”Œβ†’β”€β”€β”€β”€β”€β”β”‚
β”‚  β”‚b β”Œβ†’β”€β”β”‚β”‚
β”‚  β”‚  β”‚cdβ”‚β”‚β”‚
β”‚  β”‚  β””β”€β”€β”˜β”‚β”‚
β”‚  β””βˆŠβ”€β”€β”€β”€β”€β”˜β”‚
β””βˆŠβˆŠβ”€β”€β”€β”€β”€β”€β”€β”€β”˜

I wonder when this change has happened (and what was the motivation)?

Perhaps(?) defining β—Œ/v merely as v₁ β—Œ vβ‚‚ β—Œ β‹― β—Œ vβ‚™, which (given APL rules) would parse as foldr does make some sense. On the other hand, implementing efficient foldl is rather easier.

like image 526
lelf Avatar asked Nov 25 '19 14:11

lelf


People also ask

Is fold same as reduce?

fold is a much more valuable function than reduce . You can define many different functions in terms of fold . reduce is just a subset of fold . You define your fold differently from List.

How does foldl work?

With foldl , the function argument takes a default value, the first element of the list, and returns a new default value. When the list is empty, that default value will be the result of the fold. This is admittedly a simple example.

What is Foldl Haskell?

Haskell : foldl. Description: it takes the second argument and the first item of the list and applies the function to them, then feeds the function with this result and the second argument and so on. See scanl for intermediate results.


1 Answers

A Programming Language is from 1962.

The 1963 paper Programming Notation in Systems Design has the same definition.

The 1964 paper A Common Language for Hardware, Software, and Applications quotes the definition from A Programming Language.

At the very end of the 1966 paper Conventions Governing Order of Evaluation, Iverson wrote:

  1. In the definition

     F/x ≑ x1 F x2 F x3 F ... F x⍴x
     
    the right-to-left convention leads to a more useful definition for nonassociative functions F than does the left-to-right convention. For example, -/x denotes the alternating sum of the components of x , whereas in a left-to-right convention it would denote the first component minus the sum of the remaining components. Thus if d is the vector of decimal digits representing the number n , then the value of the expression 0=9|+/d determines the divisibility of n by 9 ; in the right-to-left convention, the similar expression 0=11|-/d determines divisibility by 11 .

like image 121
AdΓ‘m Avatar answered Oct 22 '22 00:10

AdΓ‘m