From 29509cedb9af2715328e44c481738a9ba05cff73 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Wed, 3 Nov 2021 15:51:15 -0400 Subject: =?UTF-8?q?Use=20=E2=8B=88=20rather=20than=20=E2=89=8D=E2=97=8B

Right-to-left

The functions we've shown so far are associative (ignoring floating point imprecision), meaning it's equally valid to combine elements of the argument list in any order. But it can be useful to fold using a non-associative function. In this case you must know that Fold performs a right fold, starting from the end of the array and working towards the beginning.

-↗️
    <´ "abcd"
+↗️
    ´ "abcd"
 ⟨ 'a' ⟨ 'b' "cd" ⟩ ⟩
 
-    'a' < 'b' < 'c' < 'd'  # Expanded form
+    'a'  'b'  'c'  'd'  # Expanded form
 ⟨ 'a' ⟨ 'b' "cd" ⟩ ⟩
 
-

Using the pair function < as an operand shows the structure nicely. This fold first pairs the final two characters 'c' and 'd', then pairs 'b' with that and so on. This matches BQN's right-to-left order of evaluation. More declaratively we might say that each character is paired with the result of folding over everything to its right.

+

Using Pair () as an operand shows the structure nicely. This fold first pairs the final two characters 'c' and 'd', then pairs 'b' with that and so on. This matches BQN's right-to-left order of evaluation. More declaratively we might say that each character is paired with the result of folding over everything to its right.

BQN doesn't provide a left Fold (` is Scan). However, you can fold from the left by reversing () the argument list and also reversing (˜) the operand function's argument order.

-↗️
    <˜´  "abcd"
+↗️
    ˜´  "abcd"
 ⟨ ⟨ "ab" 'c' ⟩ 'd' ⟩
 

One consequence of this ordering is that folding with Minus (-) gives an alternating sum, where the first value is added, the second subtracted, the third added, and so on. Similarly, ÷ gives an alternating product, with some elements multiplied and some divided.

@@ -151,7 +151,7 @@

The operand +÷ is a quick way to compute a continued fraction's value from a list of numbers. Here are a few terms from the continued fraction for e.

↗️
    +÷´ 21211411
-2.7183098591549295
+2.71830985915493
 

Initial element

When the operand isn't just an arithmetic primitive, folding with no initial element can be dangerous. Even if you know 𝕩 isn't empty, saving you from an "Identity not found" error, the case with only one element can easily violate expectations. Here's a somewhat silly example of a function meant to merge elements of the argument into a single list (∾⥊¨ is a much better way to do this):

@@ -206,7 +206,7 @@ ⟨ 0 0 0 0 ⟩

Just like Fold, Insert allows an initial element for the left argument, so that you don't need to rely on the interpreter knowing the identity. A more complete translation into Fold is therefore {𝕨𝔽´<˘𝕩}. The expression below shows that the operand function is called on the last major cell when the identity, then the next-to-last major cell and so on. In total there are 𝕩 calls, while there would be 1-˜𝕩 without the left argument.

-↗️
    "id" <˝ "row0 ""row1 ""row2 "
+↗️
    "id" ˝ "row0 ""row1 ""row2 "
 ┌─                                      
 · "row0 " ⟨ "row1 " ⟨ "row2 " "id" ⟩ ⟩  
                                        ┘
-- 
cgit v1.2.3