(fold-right / 1 (list 1 2 3)) is computed as \(1 / (2 / (3/1))\) which gives
(fold-left / 1 (list 1 2 3)) is computed as \(((1/1)/2)/3\) which gives
(fold-right list nil (list 1 2 3)) gives
(1 (2 (3 ()))).
(fold-left list nil (list 1 2 3)) gives
(((() 1) 2) 3).
We can see that the order of the element are not changed with the two fold procedures, only the priority of computation changes, that’s why
op must be an associative operation in order to obtain the same result with both fold procedures. Of course we also need to be sure that the
initial argument is the neutral element of the operation.
Let’s test this property with the multiplication of two matrices seen in the previous exercise, keep in mind that matrix multiplication is associative but not commutative.
We start by defining some matrices and the identity matrix. I chose to use quotation to make it less verbose (read chapter 2.3.1 to know more about how quotation works).
Next we create a list of the matrices.
Now we can finally multiply them with both fold procedures and see if they gives the same value with
It perfectly works as expected.