diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-07-18 18:26:52 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2020-07-18 18:29:59 -0400 |
| commit | 229e2cd2f5c78b13c483a8559dead2c8f31d8e42 (patch) | |
| tree | 9d6a1ff0100bda7632948987352b3d6614c3eeb5 /docs/doc/functional.html | |
| parent | 010b97c8cf346dfeafc289ae66f77e8c61cd9865 (diff) | |
Terminology changes: subject, 1/2-modifier, Box/Unbox to Enclose/Merge, blocks
Diffstat (limited to 'docs/doc/functional.html')
| -rw-r--r-- | docs/doc/functional.html | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/docs/doc/functional.html b/docs/doc/functional.html index e070e279..bc1b7c73 100644 --- a/docs/doc/functional.html +++ b/docs/doc/functional.html @@ -2,7 +2,7 @@ <h1 id="functional-programming">Functional programming</h1> <p>BQN boasts of its functional capabilities, including first-class functions. What sort of functional support does it have, and how can a BQN programmer exercise these and out themself as a Schemer at heart?</p> <p>First, let's be clear about what the terms we're using mean. A language has <em>first-class functions</em> when functions (however they are defined) can be used in all the same ways as "ordinary" values like numbers and so on, such as being passed as an argument or placed in a list. Lisp and JavaScript have first-class functions, C has unsafe first-class functions via function pointers, and Java and APL don't have them as functions can't be placed in lists or used as arguments. This doesn't mean every operation is supported on functions: for instance, numbers can be added, compared, and sorted; while functions could perhaps be added to give a train, comparing or sorting them as functions (not representations) isn't computable, and BQN doesn't support any of the three operations when passing functions as arguments.</p> -<p>Traditionally APL has worked around its lack of first-class functions with operators or second-order functions. Arrays in APL are first class while functions are second class and operators are third class, and each class can act on the ones before it. However, the three-tier system has some obvious limitations that we'll discuss, and BQN removes these by making every type first class.</p> +<p>Traditionally, APL has worked around its lack of first-class functions with operators, that is, second-order functions. Arrays in APL are first class while functions are second class and operators are third class, and each class can act on the ones before it. However, the three-tier system has some obvious limitations that we'll discuss, and BQN removes these by making every type first class.</p> <p>The term <em>functional programming</em> is more contentious, and has many meanings some of which can be vague. Here I use it for what might be called <em>first-class functional programming</em>, programming that makes significant use of first-class functions; in this usage, Scheme is probably the archetypal functional programming language. However, two other definitions are also worth mentioning. APL is often called a functional programming language on the grounds that functions can be assigned and manipulated, and called recursively, all characteristics it shares with Lisp. I prefer the term <em>function-level programming</em> for this usage. A newer usage, which I call <em>pure functional programming</em>, restricts the term "function" to mathematical functions, which have no side effects, so that functional programming is programming with no side effects, often using monads to accumulate effects as part of arguments and results instead. Finally, <em>typed functional programming</em> is closely associated with pure functional programming and refers to statically-typed functional languages such as Haskell, F#, and Idris (the last of which even supports <em>dependently-typed functional programming</em>, but I already said "finally" so we'll stop there). Of these, BQN supports first-class functional and function-level programming, allows but doesn't encourage pure functional programming, and does not support typed functional programming, as it is dynamically and not statically typed.</p> <p>Another topic we are interested in is <em>lexical scoping</em> and <em>closures</em>. Lexical scoping means that the realm in which a variable exists is determined by its containing context (in BQN, the surrounding set of curly braces <code><span class='Brace'>{}</span></code>, if any) within the source code. A closure is really an implementation mechanism, but it's often used to refer to a property of lexical scoping that appears when functions defined in a particular block can be accessed after the block finishes execution. For example, they might be returned from a function or assigned to a variable outside of that function's scope. In this case the functions can still access variables in the original scope. I consider this property to be a requirement for a correct lexical scoping implementation, but it's traditionally not a part of APL: implementation might not have lexical scoping (for example, J and I believe A+ use static scoping where functions can't access variables in containing scopes) or might cut off the scope once execution ends, leading to value errors that one wouldn't predict from the rules of lexical scoping.</p> <h2 id="functions-in-apl">Functions in APL</h2> @@ -13,23 +13,23 @@ <p><em>Reminder: I am discussing only first-class functional programming here, and not other concepts like pure or typed functional programming!</em></p> <p>What does functional programming in BQN look like? How is it different from the typical APL style of manipulating functions with operators?</p> <h3 id="working-with-roles">Working with roles</h3> -<p>First, let's look at the basics: a small program that takes a function as its argument and result. The function <code><span class='Function'>Lin</span></code> below gives a linear approximation to its function argument based on the values at 0 and 1. To find these two values, we call the argument as a function by using its uppercase spelling, <code><span class='Function'>π</span></code>.</p> +<p>First, let's look at the basics: a small program that has functions as its argument and result. The function <code><span class='Function'>Lin</span></code> below gives a linear approximation to its function argument based on the values at 0 and 1. To find these two values, we call the argument as a function by using its uppercase spelling, <code><span class='Function'>π</span></code>.</p> <pre><span class='Function'>Lin</span> <span class='Gets'>β</span> <span class='Brace'>{</span> <span class='Value'>v0</span> <span class='Gets'>β</span> <span class='Function'>π</span> <span class='Number'>0</span> <span class='Value'>v0</span> <span class='Function'>+</span> <span class='Paren'>((</span><span class='Function'>π</span> <span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>-</span> <span class='Value'>v0</span><span class='Paren'>)</span> <span class='Function'>Γ</span> <span class='Function'>β’</span> <span class='Brace'>}</span> </pre> -<p>We can pass it the exponential function as an argument by giving it the name <code><span class='Function'>Exp</span></code> and then referring to it in lowercase (that is, in a value role). The result is a train that adds 1 to <em>e</em>-1 times the argument.</p> +<p>We can pass it the exponential function as an argument by giving it the name <code><span class='Function'>Exp</span></code> and then referring to it in lowercase (that is, in a subject role). The result is a train that adds 1 to <em>e</em>-1 times the argument.</p> <pre> <span class='Function'>Exp</span> <span class='Gets'>β</span> <span class='Function'>β</span> <span class='Function'>Lin</span> <span class='Value'>exp</span> <span class='Paren'>(</span><span class='Number'>1</span> <span class='Function'>+</span> <span class='Paren'>(</span><span class='Number'>1.71828182845905</span> <span class='Function'>Γ</span> <span class='Function'>β’</span><span class='Paren'>))</span> </pre> -<p>As with all functions, the result of <code><span class='Function'>Lin</span></code> has a value role. To use it as a function, we give it a name and then use that name with an uppercase spelling.</p> +<p>As with all functions, the result of <code><span class='Function'>Lin</span></code> has a subject role. To use it as a function, we give it a name and then use that name with an uppercase spelling.</p> <pre> <span class='Value'>expLin</span> <span class='Gets'>β</span> <span class='Function'>Lin</span> <span class='Value'>exp</span> <span class='Function'>ExpLin</span> <span class='Number'>5</span> <span class='Number'>9.59140914229523</span> </pre> -<p>A tricker but more compact method is to use the modifier <code><span class='Brace'>{</span><span class='Function'>π½</span><span class='Brace'>}</span></code>, as the input to a modifier can have a value or function role but its output always has a function role.</p> +<p>A tricker but more compact method is to use the 1-modifier <code><span class='Brace'>{</span><span class='Function'>π½</span><span class='Brace'>}</span></code>, as the input to a modifier can have a subject or function role but its output always has a function role.</p> <pre> <span class='Paren'>(</span><span class='Function'>Lin</span> <span class='Value'>exp</span><span class='Paren'>)</span><span class='Brace'>{</span><span class='Function'>π½</span><span class='Brace'>}</span> <span class='Number'>5</span> <span class='Number'>9.59140914229523</span> </pre> @@ -48,27 +48,27 @@ <span class='Number'>9.59140914229523</span> </pre> <h3 id="arrays-of-functions">Arrays of functions</h3> -<p>It's very convenient to put a function in an array, which is fortunate because this is one of the most important uses of functions as values. Here's an example of an array of functions with a reduction applied to it, composing them together.</p> -<pre> <span class='Brace'>{</span><span class='Function'>π</span><span class='Composition'>β</span><span class='Function'>π</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> <span class='Function'>β</span><span class='Ligature'>βΏ</span><span class='Function'>-</span><span class='Ligature'>βΏ</span><span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Paren'>)</span> -<span class='Function'>β</span><span class='Composition'>β</span><span class='Paren'>(</span><span class='Function'>-</span><span class='Composition'>β</span><span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Paren'>))</span> +<p>It's very convenient to put a function in an array, which is fortunate because this is one of the most important uses of functions as subjects. Here's an example of an array of functions with a reduction applied to it, composing them together.</p> +<pre> <span class='Brace'>{</span><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>π</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> <span class='Function'>β</span><span class='Ligature'>βΏ</span><span class='Function'>-</span><span class='Ligature'>βΏ</span><span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Paren'>)</span> +<span class='Function'>β</span><span class='Modifier2'>β</span><span class='Paren'>(</span><span class='Function'>-</span><span class='Modifier2'>β</span><span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Paren'>))</span> </pre> -<p>Like any function, this one can be given a name and then called. A quirk of this way of defining a function is that it has a value role (it's the result of the function <code><span class='Brace'>{</span><span class='Function'>π</span><span class='Composition'>β</span><span class='Function'>π</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span></code>) and so must be defined with a lowercase name.</p> -<pre> <span class='Value'>gauss</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Function'>π</span><span class='Composition'>β</span><span class='Function'>π</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> <span class='Function'>β</span><span class='Ligature'>βΏ</span><span class='Function'>-</span><span class='Ligature'>βΏ</span><span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Paren'>)</span> +<p>Like any function, this one can be given a name and then called. A quirk of this way of defining a function is that it has a subject role (it's the result of the function <code><span class='Brace'>{</span><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>π</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span></code>) and so must be defined with a lowercase name.</p> +<pre> <span class='Value'>gauss</span> <span class='Gets'>β</span> <span class='Brace'>{</span><span class='Function'>π</span><span class='Modifier2'>β</span><span class='Function'>π</span><span class='Brace'>}</span><span class='Modifier'>Β΄</span> <span class='Function'>β</span><span class='Ligature'>βΏ</span><span class='Function'>-</span><span class='Ligature'>βΏ</span><span class='Paren'>(</span><span class='Function'>Γ</span><span class='Modifier'>Λ</span><span class='Paren'>)</span> <span class='Function'>Gauss</span> <span class='Number'>2</span> <span class='Number'>0.0183156388887342</span> </pre> <p>Another, and probably more common, use of arrays of functions is to apply several different functions to one or more arguments. Here we apply three different functions to the number 9:</p> -<pre> <span class='Bracket'>β¨</span><span class='Function'>β</span><span class='Separator'>,</span> <span class='Number'>2</span><span class='Composition'>βΈ</span><span class='Function'>βΎ</span><span class='Separator'>,</span> <span class='Function'>β’-β</span><span class='Bracket'>β©</span> <span class='Brace'>{</span><span class='Function'>π</span><span class='Value'>π©</span><span class='Brace'>}</span><span class='Modifier'>Β¨</span> <span class='Number'>9</span> +<pre> <span class='Bracket'>β¨</span><span class='Function'>β</span><span class='Separator'>,</span> <span class='Number'>2</span><span class='Modifier2'>βΈ</span><span class='Function'>βΎ</span><span class='Separator'>,</span> <span class='Function'>β’-β</span><span class='Bracket'>β©</span> <span class='Brace'>{</span><span class='Function'>π</span><span class='Value'>π©</span><span class='Brace'>}</span><span class='Modifier'>Β¨</span> <span class='Number'>9</span> <span class='Value'>[</span> <span class='Number'>3</span> <span class='Value'>[</span> <span class='Number'>2</span> <span class='Number'>9</span> <span class='Value'>]</span> <span class='Number'>Β―8094.083927575384</span> <span class='Value'>]</span> </pre> -<p>The composition Choose (<code><span class='Composition'>βΆ</span></code>) relies on arrays of functions toβ¦ function. It's very closely related to Pick <code><span class='Function'>β</span></code>, and in fact when the left operand and the elements of the right operand are all value types there's no real difference: Choose returns the constant function <code><span class='Value'>π</span><span class='Function'>β</span><span class='Value'>π</span></code>.</p> -<pre> <span class='Number'>2</span><span class='Composition'>βΆ</span><span class='String'>"abcdef"</span> <span class='String'>"arg"</span> +<p>The 2-modifier Choose (<code><span class='Modifier2'>βΆ</span></code>) relies on arrays of functions toβ¦ function. It's very closely related to Pick <code><span class='Function'>β</span></code>, and in fact when the left operand and the elements of the right operand are all data there's no real difference: Choose returns the constant function <code><span class='Value'>π</span><span class='Function'>β</span><span class='Value'>π</span></code>.</p> +<pre> <span class='Number'>2</span><span class='Modifier2'>βΆ</span><span class='String'>"abcdef"</span><span class='Ligature'>βΏ</span><span class='String'>"arg"</span> <span class='Value'>c</span> </pre> <p>When the operands contain functions, however, the potential of Choose as a ternary-or-more operator opens up. Here's a function for a step in the Collatz sequence, which halves an even input but multiplies an odd input by 3 and adds 1. To get the sequence for a number, we can apply the same function many times. It's an open problem whether the sequence always ends with the repetition 4, 2, 1, but it can take a surprisingly long time to get thereβtry 27 as an argument.</p> -<pre> <span class='Paren'>(</span><span class='Number'>2</span><span class='Composition'>βΈ</span><span class='Function'>|</span><span class='Paren'>)</span><span class='Composition'>βΆ</span><span class='Bracket'>β¨</span><span class='Function'>Γ·</span><span class='Composition'>β</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Function'>+</span><span class='Number'>3</span><span class='Function'>Γβ’</span><span class='Bracket'>β©</span><span class='Modifier'>Β¨</span> <span class='Number'>6</span><span class='Ligature'>βΏ</span><span class='Number'>7</span> +<pre> <span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>βΈ</span><span class='Function'>|</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Bracket'>β¨</span><span class='Function'>Γ·</span><span class='Modifier2'>β</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Function'>+</span><span class='Number'>3</span><span class='Function'>Γβ’</span><span class='Bracket'>β©</span><span class='Modifier'>Β¨</span> <span class='Number'>6</span><span class='Ligature'>βΏ</span><span class='Number'>7</span> <span class='Value'>[</span> <span class='Number'>3</span> <span class='Number'>22</span> <span class='Value'>]</span> - <span class='Paren'>(</span><span class='Number'>2</span><span class='Composition'>βΈ</span><span class='Function'>|</span><span class='Paren'>)</span><span class='Composition'>βΆ</span><span class='Bracket'>β¨</span><span class='Function'>Γ·</span><span class='Composition'>β</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Function'>+</span><span class='Number'>3</span><span class='Function'>Γβ’</span><span class='Bracket'>β©</span><span class='Composition'>β</span><span class='Paren'>(</span><span class='Function'>β</span><span class='Number'>10</span><span class='Paren'>)</span> <span class='Number'>6</span> + <span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>βΈ</span><span class='Function'>|</span><span class='Paren'>)</span><span class='Modifier2'>βΆ</span><span class='Bracket'>β¨</span><span class='Function'>Γ·</span><span class='Modifier2'>β</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>1</span><span class='Function'>+</span><span class='Number'>3</span><span class='Function'>Γβ’</span><span class='Bracket'>β©</span><span class='Modifier2'>β</span><span class='Paren'>(</span><span class='Function'>β</span><span class='Number'>10</span><span class='Paren'>)</span> <span class='Number'>6</span> <span class='Value'>[</span> <span class='Number'>6</span> <span class='Number'>3</span> <span class='Number'>10</span> <span class='Number'>5</span> <span class='Number'>16</span> <span class='Number'>8</span> <span class='Number'>4</span> <span class='Number'>2</span> <span class='Number'>1</span> <span class='Number'>4</span> <span class='Value'>]</span> </pre> |
