Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Functional programming
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Comparison to logic programming == [[Logic programming]] can be viewed as a generalisation of functional programming, in which functions are a special case of relations.<ref>{{cite book|author1=Daniel Friedman|author2=William Byrd|author3=Oleg Kiselyov|author4=Jason Hemann|title=The Reasoned Schemer, Second Edition|year=2018|publisher=The MIT Press}}</ref> For example, the function, mother(X) = Y, (every X has only one mother Y) can be represented by the relation mother(X, Y). Whereas functions have a strict input-output pattern of arguments, relations can be queried with any pattern of inputs and outputs. Consider the following logic program: <syntaxhighlight lang="prolog"> mother(charles, elizabeth). mother(harry, diana). </syntaxhighlight> The program can be queried, like a functional program, to generate mothers from children: <syntaxhighlight lang="prolog"> ?- mother(harry, X). X = diana. ?- mother(charles, X). X = elizabeth. </syntaxhighlight> But it can also be queried ''backwards'', to generate children: <syntaxhighlight lang="prolog"> ?- mother(X, elizabeth). X = charles. ?- mother(X, diana). X = harry. </syntaxhighlight> It can even be used to generate all instances of the mother relation: <syntaxhighlight lang="prolog"> ?- mother(X, Y). X = charles, Y = elizabeth. X = harry, Y = diana. </syntaxhighlight> Compared with relational syntax, functional syntax is a more compact notation for nested functions. For example, the definition of maternal grandmother in functional syntax can be written in the nested form: <syntaxhighlight lang="prolog"> maternal_grandmother(X) = mother(mother(X)). </syntaxhighlight> The same definition in relational notation needs to be written in the unnested form: <syntaxhighlight lang="prolog"> maternal_grandmother(X, Y) :- mother(X, Z), mother(Z, Y). </syntaxhighlight> Here <code>:-</code> means ''if'' and <code> , </code>means ''and''. However, the difference between the two representations is simply syntactic. In [[Ciao (programming language)|Ciao]] Prolog, relations can be nested, like functions in functional programming:<ref>A. Casas, D. Cabeza, M. V. Hermenegildo. A Syntactic Approach to Combining Functional Notation, Lazy Evaluation and Higher-Order in LP Systems. The 8th International Symposium on Functional and Logic Programming (FLOPS'06), pages 142-162, April 2006.</ref> <syntaxhighlight lang="prolog"> grandparent(X) := parent(parent(X)). parent(X) := mother(X). parent(X) := father(X). mother(charles) := elizabeth. father(charles) := phillip. mother(harry) := diana. father(harry) := charles. ?- grandparent(X,Y). X = harry, Y = elizabeth. X = harry, Y = phillip. </syntaxhighlight> Ciao transforms the function-like notation into relational form and executes the resulting logic program using the standard Prolog execution strategy.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Functional programming
(section)
Add topic