Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Functional programming
(section)
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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)