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
Computer program
(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!
===Declarative languages=== {{main|Declarative programming}} ''Imperative languages'' have one major criticism: assigning an expression to a ''non-local'' variable may produce an unintended [[Side effect (computer science)|side effect]].<ref name="cpl_3rd-ch9-218">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 218 | isbn = 0-201-71012-9 }}</ref> [[Declarative language]]s generally omit the assignment statement and the control flow. They describe ''what'' computation should be performed and not ''how'' to compute it. Two broad categories of declarative languages are [[functional language]]s and [[Logic programming|logical languages]]. The principle behind a ''functional language'' is to use [[lambda calculus]] as a guide for a well defined [[Semantics (computer science)|semantic]].<ref name="cpl_3rd-ch9-217">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 217 | isbn = 0-201-71012-9 }}</ref> In mathematics, a function is a rule that maps elements from an ''expression'' to a range of ''values''. Consider the function: <code>times_10(x) = 10 * x</code> The ''expression'' <code>10 * x</code> is mapped by the function <code>times_10()</code> to a range of ''values''. One ''value'' happens to be 20. This occurs when x is 2. So, the application of the function is mathematically written as: <code>times_10(2) = 20</code> A ''functional language'' compiler will not store this value in a variable. Instead, it will ''push'' the value onto the computer's [[Call stack|stack]] before setting the [[program counter]] back to the calling function. The calling function will then ''pop'' the value from the stack.<ref name="dsa-ch3-p103">{{cite book | last = Weiss | first = Mark Allen | title = Data Structures and Algorithm Analysis in C++ | publisher = Benjamin/Cummings Publishing Company, Inc. | year = 1994 | page = 103 | isbn = 0-8053-5443-3 | quote = When there is a function call, all the important information needs to be saved, such as register values (corresponding to variable names) and the return address (which can be obtained from the program counter)[.] ... When the function wants to return, it ... restores all the registers. It then makes the return jump. Clearly, all of this work can be done using a stack, and that is exactly what happens in virtually every programming language that implements recursion. }}</ref> ''Imperative languages'' do support functions. Therefore, ''functional programming'' can be achieved in an imperative language, if the programmer uses discipline. However, a ''functional language'' will force this discipline onto the programmer through its syntax. Functional languages have a syntax tailored to emphasize the ''what''.<ref name="cpl_3rd-ch9-230">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 230 | isbn = 0-201-71012-9 }}</ref> A functional program is developed with a set of primitive functions followed by a single driver function.<ref name="cpl_3rd-ch9-218"/> Consider the [[Snippet (programming)|snippet]]: <code>function max( a, b ){/* code omitted */}</code> <code>function min( a, b ){/* code omitted */}</code> <code>function range( a, b, c ) {</code> :<code>return max( a, max( b, c ) ) - min( a, min( b, c ) );</code> <code>}</code> The primitives are <code>max()</code> and <code>min()</code>. The driver function is <code>range()</code>. Executing: <code>put( range( 10, 4, 7) );</code> will output 6. ''Functional languages'' are used in [[computer science]] research to explore new language features.<ref name="cpl_3rd-ch9-240">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 240 | isbn = 0-201-71012-9 }}</ref> Moreover, their lack of side-effects have made them popular in [[parallel programming]] and [[concurrent programming]].<ref name="cpl_3rd-ch9-241">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 241 | isbn = 0-201-71012-9 }}</ref> However, application developers prefer the [[object-oriented programming|object-oriented features]] of ''imperative languages''.<ref name="cpl_3rd-ch9-241"/> ====Lisp==== [[Lisp (programming language)|Lisp]] (1958) stands for "LISt Processor".<ref name="ArtOfLisp">{{cite book | last1=Jones | first1=Robin | last2=Maynard | first2=Clive | last3=Stewart | first3=Ian | title=The Art of Lisp Programming | date=December 6, 2012 | publisher=Springer Science & Business Media | isbn=9781447117193 | page=2}}</ref> It is tailored to process [[List (abstract data type)|lists]]. A full structure of the data is formed by building lists of lists. In memory, a [[tree data structure]] is built. Internally, the tree structure lends nicely for [[Recursion (computer science)|recursive]] functions.<ref name="cpl_3rd-ch9-220">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 220 | isbn = 0-201-71012-9 }}</ref> The syntax to build a tree is to enclose the space-separated [[Element (mathematics)|elements]] within parenthesis. The following is a [[list]] of three elements. The first two elements are themselves lists of two elements: <code>((A B) (HELLO WORLD) 94)</code> Lisp has functions to extract and reconstruct elements.<ref name="cpl_3rd-ch9-221">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 221 | isbn = 0-201-71012-9 }}</ref> The function <code>head()</code> returns a list containing the first element in the list. The function <code>tail()</code> returns a list containing everything but the first element. The function <code>cons()</code> returns a list that is the concatenation of other lists. Therefore, the following expression will return the list <code>x</code>: <code>cons(head(x), tail(x))</code> One drawback of Lisp is when many functions are nested, the parentheses may look confusing.<ref name="cpl_3rd-ch9-230"/> Modern Lisp [[Integrated development environment|environments]] help ensure parenthesis match. As an aside, Lisp does support the ''imperative language'' operations of the assignment statement and goto loops.<ref name="cpl_3rd-ch9-229">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 229 | isbn = 0-201-71012-9 }}</ref> Also, ''Lisp'' is not concerned with the [[datatype]] of the elements at compile time.<ref name="cpl_3rd-ch9-227">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 227 | isbn = 0-201-71012-9 }}</ref> Instead, it assigns (and may reassign) the datatypes at [[Runtime (program lifecycle phase)|runtime]]. Assigning the datatype at runtime is called [[Name binding#Binding time|dynamic binding]].<ref name="cpl_3rd-ch9-222">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 222 | isbn = 0-201-71012-9 }}</ref> Whereas dynamic binding increases the language's flexibility, programming errors may linger until late in the [[software development process]].<ref name="cpl_3rd-ch9-222"/> Writing large, reliable, and readable Lisp programs requires forethought. If properly planned, the program may be much shorter than an equivalent ''imperative language'' program.<ref name="cpl_3rd-ch9-230"/> ''Lisp'' is widely used in [[artificial intelligence]]. However, its usage has been accepted only because it has ''imperative language'' operations, making unintended side-effects possible.<ref name="cpl_3rd-ch9-241"/> ====ML==== [[ML (programming language)|ML]] (1973)<ref name="Gordon1996">{{cite web | last = Gordon | first = Michael J. C. | author-link = Michael J. C. Gordon | year = 1996 | title = From LCF to HOL: a short history | url = http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html | access-date = 2021-10-30 | archive-date = 2016-09-05 | archive-url = https://web.archive.org/web/20160905201847/http://www.cl.cam.ac.uk/~mjcg/papers/HolHistory.html | url-status = live }}</ref> stands for "Meta Language". ML checks to make sure only data of the same type are compared with one another.<ref name="cpl_3rd-ch9-233">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 233 | isbn = 0-201-71012-9 }}</ref> For example, this function has one input parameter (an integer) and returns an integer: {{sxhl|2=sml|1=fun times_10(n : int) : int = 10 * n;}} ''ML'' is not parenthesis-eccentric like ''Lisp''. The following is an application of <code>times_10()</code>: times_10 2 It returns "20 : int". (Both the results and the datatype are returned.) Like ''Lisp'', ''ML'' is tailored to process lists. Unlike ''Lisp'', each element is the same datatype.<ref name="cpl_3rd-ch9-235">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 235 | isbn = 0-201-71012-9 }}</ref> Moreover, ''ML'' assigns the datatype of an element at [[compile time]]. Assigning the datatype at compile time is called [[Name binding#Binding time|static binding]]. Static binding increases reliability because the compiler checks the context of variables before they are used.<ref name="cpl_3rd-ch3-55">{{cite book | last = Wilson | first = Leslie B. | title = Comparative Programming Languages, Third Edition | publisher = Addison-Wesley | year = 2001 | page = 55 | isbn = 0-201-71012-9 }}</ref> ====Prolog==== [[Prolog]] (1972) stands for "PROgramming in LOGic". It is a [[logic programming]] language, based on formal [[logic]]. The language was developed by [[Alain Colmerauer]] and Philippe Roussel in Marseille, France. It is an implementation of [[SLD resolution|Selective Linear Definite clause resolution]], pioneered by [[Robert Kowalski]] and others at the [[University of Edinburgh]].<ref>{{Cite journal | publisher = Association for Computing Machinery | doi = 10.1145/155360.155362 | first1 = A. | last1 = Colmerauer | first2 = P. | last2 = Roussel | title = The birth of Prolog | journal = ACM SIGPLAN Notices | volume = 28 | issue = 3 | page = 5 | year = 1992 | url=http://alain.colmerauer.free.fr/alcol/ArchivesPublications/PrologHistory/19november92.pdf}}</ref> The building blocks of a Prolog program are ''facts'' and ''rules''. Here is a simple example: <syntaxhighlight lang=prolog> cat(tom). % tom is a cat mouse(jerry). % jerry is a mouse animal(X) :- cat(X). % each cat is an animal animal(X) :- mouse(X). % each mouse is an animal big(X) :- cat(X). % each cat is big small(X) :- mouse(X). % each mouse is small eat(X,Y) :- mouse(X), cheese(Y). % each mouse eats each cheese eat(X,Y) :- big(X), small(Y). % each big animal eats each small animal </syntaxhighlight> After all the facts and rules are entered, then a question can be asked: : Will Tom eat Jerry? <syntaxhighlight lang=prolog> ?- eat(tom,jerry). true </syntaxhighlight> The following example shows how Prolog will convert a letter grade to its numeric value: <syntaxhighlight lang="prolog"> numeric_grade('A', 4). numeric_grade('B', 3). numeric_grade('C', 2). numeric_grade('D', 1). numeric_grade('F', 0). numeric_grade(X, -1) :- not X = 'A', not X = 'B', not X = 'C', not X = 'D', not X = 'F'. grade('The Student', 'A'). </syntaxhighlight> <syntaxhighlight lang="prolog"> ?- grade('The Student', X), numeric_grade(X, Y). X = 'A', Y = 4 </syntaxhighlight> Here is a comprehensive example:<ref name="Logical English">Kowalski, R., Dávila, J., Sartor, G. and Calejo, M., 2023. Logical English for law and education. In Prolog: The Next 50 Years (pp. 287–299). Cham: Springer Nature Switzerland.</ref> 1) All dragons billow fire, or equivalently, a thing billows fire if the thing is a dragon: <syntaxhighlight lang="prolog"> billows_fire(X) :- is_a_dragon(X). </syntaxhighlight> 2) A creature billows fire if one of its parents billows fire: <syntaxhighlight lang="prolog"> billows_fire(X) :- is_a_creature(X), is_a_parent_of(Y,X), billows_fire(Y). </syntaxhighlight> 3) A thing X is a parent of a thing Y if X is the mother of Y or X is the father of Y: <syntaxhighlight lang="prolog"> is_a_parent_of(X, Y):- is_the_mother_of(X, Y). is_a_parent_of(X, Y):- is_the_father_of(X, Y). </syntaxhighlight> 4) A thing is a creature if the thing is a dragon: <syntaxhighlight lang="prolog"> is_a_creature(X) :- is_a_dragon(X). </syntaxhighlight> 5) Norberta is a dragon, and Puff is a creature. Norberta is the mother of Puff. <syntaxhighlight lang="prolog"> is_a_dragon(norberta). is_a_creature(puff). is_the_mother_of(norberta, puff). </syntaxhighlight> Rule (2) is a [[Recursion (computer science)|recursive]] (inductive) definition. It can be understood declaratively, without the need to understand how it is executed. Rule (3) shows how [[Function (computer programming)|functions]] are represented by using relations. Here, the mother and father functions ensure that every individual has only one mother and only one father. Prolog is an untyped language. Nonetheless, [[Inheritance (object-oriented programming)|inheritance]] can be represented by using predicates. Rule (4) asserts that a creature is a superclass of a dragon. Questions are answered using [[backward reasoning]]. Given the question: <syntaxhighlight lang="prolog"> ?- billows_fire(X). </syntaxhighlight> Prolog generates two answers : <syntaxhighlight lang="prolog"> X = norberta X = puff </syntaxhighlight> Practical applications for Prolog are [[knowledge representation]] and [[problem solving]] in [[artificial intelligence]].
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)