Tacit programming: Difference between revisions

Content deleted Content added
WP:REFerences: plain text + inline WP:EXTernal links > WP:CITations; parameters: adds, fills, update-standardizes, respaces. WP:LINKs: adds, update-standardize, needless-WP:PIPE > WP:NOPIPE. Small WP:COPYEDITs WP:EoS: WP:TERSE, clarify, 1st MOS:PERSON we cut+fix, MOS:NOTETHAT cut. WP:BADEMPHASIS MOS:QUOTEMARKS > Template:Codes. Cut needless carriage returns in sentences. Adds: WP:CATEGORYs, MOS:COMMENT.
Functional programming: base case before the recursive case
 
(4 intermediate revisions by one other user not shown)
Line 22:
</syntaxhighlight>
 
For a more complex example, the [[Haskell]] code {{<code|1=>p = ((.) f) . g}}</code> can be translated as:
 
<syntaxhighlight lang="python">
p = partial(compose, partial(compose, f), g)
</syntaxhighlight>
 
===Functional programming===
A simple example (in [[Haskell]]) is a program which computes the sum of a list of numbers. We can define the sum function recursively using a ''pointed'' style (cf. [[value-level programming|''value''-level programming]]) as:
<syntaxhighlight lang="haskell">sum [] = 0
sum (x:xs) = x + sum xs</syntaxhighlight>
sum [] = 0
</syntaxhighlight>
 
However, using a [[fold (higher-order function)|fold]], this can be replaced with:
Line 124 ⟶ 121:
 
In Unix scripting the functions are computer programs which receive data from [[Standard streams|standard input]] and send the results to [[Standard streams|standard output]]. For example,
 
<syntaxhighlight lang="bash">
sort | uniq -c | sort -rn
 
</syntaxhighlight>
is a tacit or point-free composition which returns the counts of its arguments and the arguments, in the order of decreasing counts. The {{Codemono|sort}} and {{Codemono|uniq}} are the functions, the {{Code|-c}} and {{Code|-rn}} control the functions, but the arguments are not mentioned. The pipe {{<Code>||}}</code> is the composition operator.
 
Due to the way pipelines work, it is normally possible only to pass one ''argument'' at a time in the form of a pair of standard [[input/output]] stream. Although extra [[file descriptor]]s can be opened from [[named pipe]]s, this no longer constitutes a point-free style.
Line 133 ⟶ 130:
====jq====
 
[[jq (programming language)|jq]] is a [[JSON]]-oriented programming language in which the {{Code<code>||}}</code> symbol is used to connect filters to form a pipeline in a familiar way. For example:
 
[1,2] | add
Line 139 ⟶ 136:
evaluates to 3. (Yes, the JSON array is a jq filter that evaluates to an array.)
 
Although similar to Unix pipelines, jq pipelines allow the incoming data to be sent to more than one recipient on the RHS of the '<code>|'</code> as though in parallel. For example, the program `<code>add/length`</code> will compute the average of the numbers in an array, so that:
 
[1,2] | add/length
Line 151 ⟶ 148:
evaluates to [2,3,1.5]
 
A dot ('<code>.'</code>) can be used to define an attachment point on the RHS, e.g.:
 
1 | [., .]
Line 161 ⟶ 158:
2 | pow(.; .)
 
evaluates to 4 since <code>pow(x;y)</code> is x to the power y.
 
=====Fibonacci sequence=====
Line 169 ⟶ 166:
[0,1] | recurse( [last, add] ) | first
 
Here, <code>[0,1]</code> is the initial pair to be taken as the first two items
in the Fibonacci sequence. (The pair <code>[1,1]</code> could likewise be used for
the variant definition.)
 
The alphabetic tokens are built-in filters: `first` and `last`
emit the first and last elements of their input arrays respectively;
and `<code>recurse(f)`</code> applies a filter, f, to its input recursively.
 
jq also allows new filters to be defined in a tacit style, e.g.:
Line 190 ⟶ 187:
 
In point-free style, this can be written in Python as:
 
<syntaxhighlight lang="python">
example = compose(foo, bar, baz)
</syntaxhighlight>
 
In jq, the equivalent point-free definition would be: