Tacit programming: Difference between revisions

Content deleted Content added
m Functional programming: line highlight
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.
Line 1:
{{Short description|Programming paradigm}}
'''Tacit programming''', also called '''point-free style''', is a [[programming paradigm]] in which function definitions do not identify the [[parameter (computer science)|arguments]] (or "points") on which they operate. Instead the definitions merely [[function composition (computer science)|compose]] other functions, among which are [[Combinatory logic|combinators]] that manipulate the arguments. Tacit programming is of theoretical interest, because the strict use of composition results in programs that are well adapted for [[Equational logic|equational]] reasoning.<ref name="cunha2005">{{cite thesis |last1=Cunha |first1=Manuel Alcino Pereira da Cunha (|year=2005) [http|url=https://hdl.handle.net/1822/2869 |title=Point-free Program Calculation |degree=PhD |publisher=[[University of Minho]]}}</ref> It is also the natural style of certainsome [[programming languageslanguage]]s, including [[APL (programming language)|APL]] and its derivatives,<ref>W.{{cite Nevillebook |editor1-last=Holmes, ed|editor1-first=W. (Neville |year=2006) ''|title=Computers and People''}}</ref> and [[concatenative programming language|concatenative languages]] such as [[Forth (programming language)|Forth]]. The lack of argument naming gives point-free style a reputation of being unnecessarilyneedlessly obscure, hence the [[epithet]] "pointless style".<ref name="cunha2005"/>
 
[[Unix]] [[Command-line interface|scripting]] uses the paradigm with [[Pipeline (Unix)|pipes]].
Line 12:
return baz(bar(foo(x)))
</syntaxhighlight>
... can be written in point-free style as the composition of a sequence of functions, without parameters:<ref>{{cite web | url=http://concatenative.org/wiki/view/Concatenative%20language/Name%20code%20not%20values | title=Name code not values | publisher=Concatenative.org | accessdateaccess-date=13 September 2013}}</ref>
 
<syntaxhighlight lang="python">
Line 22:
</syntaxhighlight>
 
For a more complex example, the [[Haskell]] code {{code|1=p = ((.) f) . g}} can be translated as:
<syntaxhighlight lang="python">
p = partial(compose, partial(compose, f), g)
Line 28:
 
===Functional programming===
A simple example (in [[Haskell (programming language)|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 (x:xs) = x + sum xs
Line 34:
</syntaxhighlight>
 
However, using a [[fold (higher-order function)|fold]], wethis can replacebe thisreplaced with:
<syntaxhighlight lang="haskell">
sum xs = foldr (+) 0 xs
Line 51:
</syntaxhighlight>
 
The following Haskell-like pseudo-code[[pseudocode]] exposes how to reduce a function definition to its point-free equivalent:
<syntaxhighlight lang="haskell" line highlight="5">
p = \x -> \y -> \z -> f (g x y) z
Line 72:
<syntaxhighlight lang="haskell">
mf = (. map) . (.) . filter
</syntaxhighlight>NoteAs that, as statedsuggested previouslyalready, the points in ''point-free'' refer to the arguments, not to the use of dots; a common misconception.<ref>{{Cite web |url=https://wiki.haskell.org/Pointfree#But_pointfree_has_more_points.21 |title=Pointfree - HaskellWiki|website=wiki.haskellHaskell.org Wiki |access-date=2016-06-05}}</ref>
 
A few programs have been written to automatically convert a Haskell expression to a point-free form.
 
===APL family===
In the language [[J (programming language)|J]], the same sort of point-free code occurs in a function made to compute the average of a list (array) of numbers:
<syntaxhighlight lang=j>avg=: +/ % #</syntaxhighlight>
<code>+/</code> sums the items of the array by mapping (<code>/</code>) summation (<code>+</code>) to the array. <code>%</code> divides the sum by the number of elements (<code>#</code>) in the array.
Line 121:
===Pipelines===
====Unix pipeline====
{{mainFurther|Pipeline (Unix)}}
 
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 '{{Code|sort'}} and '{{Code|uniq'}} are the functions, the '{{Code|-c'}} and '{{Code|-rn'}} control the functions, but the arguments are not mentioned. The pipe '{{Code||'}} is the composition operator.
 
Due to the way pipelines work, it is only 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.
 
====jq====
 
[[jq (programming language)|jq]] is a [[JSON]]-oriented programming language in which the {{Code||}} symbol is used to connect filters to form a pipeline in a familiar way. For example:
the '|' symbol is used to connect filters to form a pipeline
in a familiar way. For example:
 
[1,2] | add
 
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 '|' as though in parallel. For example, the program `add/length` will compute the average of the numbers in an array, so that:
incoming data to be sent to more than one recipient on the
RHS of the '|' as though in parallel. For example, the program `add/length`
will compute the average of the numbers in an array, so that:
 
[1,2] | add/length
Line 194 ⟶ 189:
</syntaxhighlight>
 
In point-free style, this couldcan be written in Python as:
<syntaxhighlight lang="python">
example = compose(foo, bar, baz)
Line 219 ⟶ 214:
 
[[Category:Programming paradigms]]
[[Category:Programming language comparisons]]
<!-- Hidden categories below -->
[[Category:Articles with example Haskell code]]
[[Category:Articles with example Python (programming language) code]]