Declarative programming: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
m Dating maintenance tags: {{Copyedit}}
Examples: The lisp section was complete garbage (I could only see a single relevant and correct sentence, and that is not hyperbole), so I rewrote it. Removed the scheme section as it's a dialect of lisp.
Line 60:
===Lisp===
{{copyedit|section|reason=checking relevance and factual accuracy|date=December 2023}}
[[Lisp (programming language) | Lisp]] is a family of programming languages loosely inspired by mathematical notation and [[Alonzo Church]]'s [[lambda calculus]]. While some dialects, such as [[Common Lisp]] are primarily imperative, these lisps support functional programming, and other lisps, such as [[Scheme (programming language) | Scheme]] are designed for functional programming.
[[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 data structure is formed by building lists of lists. In memory, this forms a [[Tree (data structure)|tree data structure]]. Internally, the tree structure of typical Lisp data lends itself nicely to processing with [[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 whitespace-separated [[Element (mathematics)|elements]] within parenthesis. The following is a list of three elements. The first two elements are themselves lists of two elements each:
 
ForIn examplescheme, one may define the [[factorial]] function in scheme as such:
{{sxhl|2=lisp|((A B) (HELLO WORLD) 94)}}
 
<syntaxhighlight lang=scheme>
Lisp has functions to extract and reconstruct elements.<ref name="cpl_3rd-ch9-221">{{cite book
(define (factorial n)
| last = Wilson
(if (= n 0) ;;;
| first = Leslie B.
1 ;;; 0! = 1
| title = Comparative Programming Languages, Third Edition
(* n (factorial (- n 1))))) ;;; n! = n*(n-1)!
| publisher = Addison-Wesley
</syntaxhighlight>
| year = 2001
| page = 221
| isbn = 0-201-71012-9
}}</ref> The function <code>car</code> (sometimes called <code>first</code>) returns the first element in the list. The function <code>cdr</code> (sometimes called <code>rest</code>) returns a list containing everything but the first element. The function <code>cons</code> returns a list that is the second argument with the first prepended to it. Therefore, if X is a non-empty list, the following expression evaluates to the list <code>X</code>:
 
This can be construed as defining the factorial function using its recursive mathematical definition, as opposed to simply defining a procedure, as one would typically do in an imperative language.
{{sxhl|2=lisp|(cons (car x) (cdr x))}}
 
In lisps, as in lambda calculus, functions are generally [[first-class citizen | first-class citizens]]. Loosely, this means that functions can return functions, and be used as parameters for other functions.
The code itself returns a ''copy'' of the list <code>X</code>. As is typical in functional languages, operations in Lisp often copy data when asked to produce new data from old. The tree structure of Lisp data facilitates this as well: a new structure made from pre-existing data shares as much of its internal structure with its antecedents as possible, with new additions stored as branches of the tree, and references to the original structure under its original name will return exactly that and no more.
 
This can greatly simplify the definition of certain functions.
One drawback of Lisp is when many functions are nested, the parentheses may look confusing.<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> 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 [[data type|datatype]] of the elements at compile time. Instead, it assigns the datatypes at runtime. This may lead to programming errors not being detected early in the development process. To counteract this, Lisp development is typically carried out in an extremely incremental manner, with functions and higher-order functions built up and tested live during development. Additionally, through the use of [[Macro (computer science)#Syntactic macros|macro]]s, which are Lisp functions that operate upon Lisp programs as data structures, type checking may be performed discretionally at any point the programmer wishes.
 
For example, if one wants to create a function that returns the first n [[square numbers | squares]] in [[Racket (programming language) | Racket]], one can simply write the following:
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">{{cite book
 
| last = Wilson
<syntaxhighlight lang=scheme>
| first = Leslie B.
(define (first-n-squares n)
| title = Comparative Programming Languages, Third Edition
(map
| publisher = Addison-Wesley
(lambda (x) (* x x)) ;;; A function mapping x -> x^2
| year = 2001
(range n))) ;;; List of the first n non-negative integers
| page = 241
</syntaxhighlight>
| isbn = 0-201-71012-9
 
}}</ref>
[[Map (higher-order function) | Map]] in the above takes a function and a list, applying the function to each member of the list.
 
===ML===
Line 160 ⟶ 123:
| isbn = 0-201-71012-9
}}</ref>
===Scheme===
 
[[Scheme (programming language) | Scheme]] is a dialect of [[Lisp (programming language) | Lisp]] designed for functional programming.
 
Defining functions using [[functional programming]] can be very similar to defining functions as in mathematics.
 
For example, one may define the [[factorial]] function in scheme as such:
 
<syntaxhighlight lang=scheme>
(define (factorial n)
(if (= n 0) ;;;
1 ;;; 0! = 1
(* n (factorial (- n 1))))) ;;; n! = n*(n-1)!
</syntaxhighlight>