0% found this document useful (0 votes)
5 views

Programming Paradigms

The document provides an overview of declarative programming paradigms, emphasizing its focus on 'what' a program should achieve rather than 'how' to achieve it. It discusses the history, key principles, advantages, and disadvantages of declarative programming, as well as popular languages such as SQL, HTML, and Haskell. The document also contrasts declarative programming with imperative programming, highlighting its suitability for complex systems and artificial intelligence applications.

Uploaded by

ggermslayer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Programming Paradigms

The document provides an overview of declarative programming paradigms, emphasizing its focus on 'what' a program should achieve rather than 'how' to achieve it. It discusses the history, key principles, advantages, and disadvantages of declarative programming, as well as popular languages such as SQL, HTML, and Haskell. The document also contrasts declarative programming with imperative programming, highlighting its suitability for complex systems and artificial intelligence applications.

Uploaded by

ggermslayer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Declarative Programming Paradigms

Group 1: Lashauntea King, Rajunior Hamilton,


Devardo Gray, Pierce-Mikal Mcnamee
Table of Contents
Introduction 2
Programming Paradigms 3
Declarative Programming 4
History of Declarative Programming 7
Key Principles 8
Advantages & Disadvantages 10
Popular Declarative Programming Languages 11
Declarative vs. Imperative Programming 18
Conclusion 20
References 21

1 | Page
Introduction
In software development, understanding different programming
paradigms is crucial for writing efficient and maintainable code.
Programming paradigms explain how developers design or write a
software. Two of the most prevalent paradigms are imperative and
declarative programming. Declarative programming focuses on
“what” the program should do rather than “how” it should do it or
carry out steps. Despite the short mention of imperative
programming, this research’s main topic is declarative
programming.

2 | Page
Programming
Paradigms

What are Programs?


A program can be defined as a specific set of ordered operations for a computer to perform.

What are Paradigms?


A paradigm is a theory or a group of ideas about how something should be done, made or
thought about.

Programming Paradigms
Programming Paradigms can be defined as a methodology or style that guides the process of writing
code. It encompasses principles and practices that dictate how code should be structured, how data
should be organized, and how algorithms should be designed. (DiscoveringCS – Muhammad Hazeem
Mubashar – February 8, 2024)

There are several types of programming paradigms but one will be focused on: Declarative
Programming Paradigm.

3 | Page
Declarative
Programming

What is a Declaration?
A declaration in programming refers to the process of specifying the properties of a variable, function, or
other programming constructs, but not providing its implementation or initial value.

What is Declarative Programming?


Declarative programming mainly focuses on describing the desired outcome or result, rather
than specifying the exact steps needed to achieve that outcome (Pinki, S. (May 1, 2024)
Introduction to Programming Paradigm – Unlocking the basics). The developer does not
explicitly mention how to perform a task but simply declares what they want to achieve.
Moreover, declarative programming creates objects or programming components to achieve
desired outcomes. This method of programming focuses on the higher-level objectives of
software and promotes greater code readability, modularity and scalability (StudySmarter
UK- Declarative Programming).
Example of declarative programming: small_nums= [x for x in range (20) if x<5] or
$firstnames = array_values($participantlist);

4 | Page
Why use Declarative Programming?
Declarative programming focuses on describing what you want instead of describing the step by step
process of how to achieve it. Its suitable for complex search and systems and is a higher level
programming language. It is also suitable for artificial intelligence.

Various languages paradigms under Declarative


Programming Languages:
 Logic Programming Languages
 Functional Programming Languages
 Database Languages

Logic Programming Language


The logic programming language uses a form of symbolic logic as its programming. Logic can be
defined as a system of principles that uses reason to determine if a conclusion is true or untrue. Hence, it
consists of a set of statements that describe what is true about the desired result.

Syntax – eg : Prolog
Condition 1 – action 1
Condtion 2 – action 2
.
.
.
Condition (n) – action (n)

Functional Programming
The functional programming logic does all the computations on the evaluation of functions or the
application of functions. It is also known as application languages. An example of functional
programming language is Lisp.

Syntax – eg: Lisp


Function n(……(function2(function,(data))……)

Database Languages
It includes the following:
1. DDL (Data Definition Language): It is used to describe a new collection of data or facts.
2. DML(Data Manipulation Language): It is used to interact with existing databases.
3. DCL(Data Control Language): It is used to control data. It includes: Commit and Rollback

5 | Page
The three main types of declarative programming are (Rahual, A. Adam B. () declarative
programming), (Mazer.dev(February 15 ,2023 ) Programming Paradigms):

Constraint programming
Constraint programming, also known as constraint optimization, is a programming paradigm in which
constraints are declaratively stated for a set of decision variables. The arbitrary constraints help with
modelling the problem to be solved without specifying the steps to be executed.

Logic programming
Logic programming involves the use of logical sentences that express certain rules and facts as well as
logical inferences based on available data. These languages use queries to display relevant data, a base
of existing logic (predicates), and formulas that state facts (atoms).

Advantages Disadvantages
It can be more suitable for problems that involve The search space for a problem can be very large,
complex reasoning or search, such as artificial which can make the program slow and inefficient.
intelligence or expert systems
It can be more declarative and easier to read and It can be less suitable for problems that require
understand. mutable state or complex control flow, as these
features are not easily expressed in terms of logical
constraints.

Constraint logic programming


Constraint logic programs include constraints in a body of clauses. An interpreter is used for execution.
It recursively scans the clauses (with its constraints placed in a constraint store) to prove a specific goal.

6 | Page
History of
Declarative
Programming
Declarative programming can trace back to its origins, which is the mid 20-th century where
the field of computer science was still in its formative years. Declarative programming
emerged as a result of the increasing complexity of software development. Declarative
programming, as we understand it today, started taking form in the 1970s with the
development of languages like Prolog and Lisp. These languages emphasized the concept of
what needs to be achieved rather than how it needs to be achieved. (John Samuel, Declarative
Programming)

Declarative programming offers several distinct advantages. Some include better readability,
immutability, etc.

7 | Page
Key Principles
In order to fully understand what declarative programming is, it’s
important to understand its key principles. They include:
 High order abstractions
 Immutable data
 Pure fractions
 Declarative DSLs (Domain Specific Languages)

High order abstractions


High order abstractions refer to constructs that allow you to express complex logic with
simpler and more concise code.

Immutable data
Immutable data refers to data structures that cannot be changed once they have been created.
It prevents many bugs caused by unintended side effects and also provides a more
manageable memory.

Pure functions
Pure functions refers to functions that always produce the same output when given the same
input and have no side effects. It allows easier testing and debugging due to their
predictability.

Declarative DSLs
Declarative DSLs are programming languages that are designed for a specific domain which
allows you to express the domain requirements more concise and naturally.

8 | Page
Advantages &
Disadvantage
s
Advantages Disadvantages
Better readability and understanding of the More lines of code, where a potential bug
code. could ‘hide.’

Better control over the actual execution of the Longer debugging, due to bigger stack traces.
changes to the world.

Immutability – Once objects are created, they Potential loss of performance, due to more
cannot be modified. memory allocation and intermediate function
calls.

Minimized state dependencies – This reduces Developers are usually uncomfortable with
the complexity of debugging. this way of programming.

A table including the advantages & disadvantages is below (Benoit, R. (October 21, 2022)
Declarative vs imperative), (Capital One, (August 5, 2020) The Complete Guide to
Declarative Programming):

Better readability and understanding of the code


Since declarative code focuses on expressing what the desired outcome should be, the
resulting code is often straightforward and easier to understand due to its simplicity.
This also has advantages as well which can reduce error potential and can make it easier to
maintain the code.

9 | Page
Better control over the actual execution of the changes to
the world
It allows better control over how changes to the program are executed.

Immutability
Declarative programming emphasizes immutability which means that once it is created, it
cannot be altered in any way.

Minimized state dependencies


This reduces reliance on external states, simplifying debugging and lowering complexities.

10 | P a g e
Popular
Declarative
Programming
Languages
Examples of declarative programming languages are:
 SQL (Structured Query Language)
 HTML (Hyper Text Markup Language)
 CSS (Cascading Style Sheets).
 Prolog
 Haskell
 Miranda
 XQuery
 Lisp

These languages have been used mainly because of their specific domains of applications
(StudySmarter UK- Declarative Programming), (Capital One, (August 5, 2020) The
Complete Guide to Declarative Programming).

1. SQL
SQL (Structured Query Language) is designed for managing relational databases,
querying and managing data. Its main purpose is data definition and retrieval and it

11 | P a g e
enables developers to specify the data they wish to store or retrieve without specifying the
exact steps the database system should take to perform these operation.

Key features of SQL


 Declarative syntax – SQL queries are human readable and describe the desired
result not including the process to achieve it.
 Flexible querying capabilities – It offers a variety of querying data and data
manipulation features for example: filtering, joining, sorting and grouping.
 Compatibility – It is mostly supported by relational database management
systems, enabling it to be a widely used standard for interacting with databases.

2. HTML
HTML (Hyper Text Markup Language) is the second declarative language mentioned
and its purpose is for structuring content on the web. Its aim is focusing on defining
the structure and semantics of web pages, allowing developers to describe elements
such as headings, paragraphs, links and images without specifying how these
elements should be rendered by the web.

Key features of HTML


 Markup elements – It uses a series of tags and attributes to define the
structure and content of the web pages.

12 | P a g e
 Semantic value – The HTML tags provide meaningful information about the
content they enclose, allowing browsers, search engines and other systems to
interpret the page more efficiently.
 Accessibility – HTML supports a variety of attributes and features allowing it
to be more accessible to users with disabilities.

3. CSS
CSS (Cascading Style Sheets) is a declarative language style used to style and format
HTML documents. It enables developers to describe the visual presentation of web
pages, including aspects such as layout, colours, format and animations without
detailing the specific steps the browser should take to apply these styles.

Key features of CSS


 Separation of concerns – CSS promotes the separation of content allowing
developers to maintain and update styles without the use of HTML.

13 | P a g e
 Selector syntax – CSS uses a powerful and flexible syntax to target specific
HTML elements and apply styles to them.
 Responsive designs – It allows developers to create responsive web designs
that adapt to certain conditions (screen size, different devices, resolution, etc).

4. Prolog
Prolog is a declarative programming language that is designed for developing logic bases
AI (Artificial Intelligence) applications. Since developers cannot se rules and facts around
a problem, Prologs interpreter will use that information and automatically infer solutions.
It allows the programmer to specify the rules and facts about a domain and then the
interpreter will automatically work out solutions. (Bulletin (November 1, 2023)
Introduction to Prolog: A Programming Language for AI)

5.

Haskell
Haskell is a purely functional programming language, meaning that instead of giving the
computer a sequence of tasks to execute (as in imperative programming), you define what
things are and how they relate through functions. In Haskell, once you set a value, it is
immutable variables cannot be reassigned. Functions in Haskell have no side effects,
which ensures that they always return the same result when given the same inputs
(referential transparency). This property makes Haskell programs easy to reason about,
and it allows for easier verification and composition of complex functions.

14 | P a g e
Haskell is also lazy, meaning that computations are deferred until their results are needed.
This allows for efficient processing and the use of infinite data structures. For example,
functions like `doubleMe` can be applied to a list of numbers, but the computation only
occurs when the result is actually requested, avoiding unnecessary work.

Additionally, Haskell is statically typed with type inference, meaning the compiler knows
the types of data (e.g., numbers, strings) at compile time, catching many errors before the
program runs. The type system ensures strong correctness guarantees while allowing
developers to write less explicit type information.
Features of Haskell
1. Purely functional language: emphasizes immutability and predictable outputs.
2. Lazy evaluation: optimize memory usage and enable infinite data structures.
3. Strong static type system: catches errors at compile time and enhances code safety.
4. Pattern matching: allows concise handling of complex data structures and conditional
logic.
- Code Example:
haskell
factorial 0 = 1
factorial n = n*factorial (n - 1)

6. Miranda
Miranda, a predecessor to Haskell, introduced many features common in modern functional
languages, such as pattern matching, higher-order functions, and lazy evaluation. It embraces
equational reasoning by allowing programs to be described as a series of equations that define
functions and expressions. A concise example is the creation of an infinite list of Fibonacci
numbers using a mathematical expression. While Miranda itself is no longer widely used, its
influence can be seen in many contemporary functional languages.
1. Purely Functional: Miranda emphasizes functions without side effects, using immutable
data and declarative syntax.
2. Lazy Evaluation: Values are computed only when needed, enabling efficient memory
usage and infinite data structures.
3. Pattern Matching and List Comprehensions: Supports concise, readable definitions for
handling data structures and constructing lists.

15 | P a g e
4. Strong Typing and Type Inference: Ensures type safety with minimal need for explicit
type annotations.
- Code Example:
Miranda
fibs = 0 : 1 : [a + b | (a, b) <- zip fibs (tail fibs)]

7. XQUERY
XQuery is a declarative query language specifically designed for querying and
transforming XML data. With XQuery, users specify what data to retrieve or transform
without having to detail the process of traversing or manipulating data nodes. This
approach, similar in style to SQL, leverages functions, expressions, and extensive use of
XPath for addressing parts of XML documents. A common example is an XQuery
statement that extracts titles of books priced above a specified amount, focusing on the
desired result rather than traversal logic. XQuery is heavily utilized in web services, XML
document processing, and data integration tasks.
It simplifies complex tasks by replacing proprietary middleware and programming
languages such as Java and C++ with concise, easy-to-learn syntax. XQuery is used for a
variety of applications, including searching academic articles, business databases, medical
records, and scientific images. Notable examples include sites like the Oxford African
American Studies Center and Springer Images, which use XQuery to power search and
data retrieval functionalities. XQuery’s flexibility allows it to unify access to diverse data
formats and sources, making it a powerful tool for integrating XML data from different
systems.

XQuery supports a broad range of features, including support for external XML Schema,
static typing, and the ability to fetch data from relational databases or other data sources
via HTTP. It has become a key tool for developers seeking to streamline data processing
and query tasks. With over 40 different software packages supporting XQuery, developers
can choose from a range of options based on factors like platform, price, and
performance. Additionally, the W3C provides extensive documentation, tutorials, and
mailing lists to support learning and implementation of the language. The XQuery
community continues to grow, with resources available for both beginners and
experienced implementers.
Example Query:
xquery
for $x in doc("books.xml")//book

16 | P a g e
where $x/price > 30
return $x/title

8. Lisp
Lisp is a programming language known for its versatility, supporting multiple
paradigms, including declarative, functional, and imperative programming. In the
context of declarative programming, Lisp allows for the expression of computations
without focusing on control flow, relying on recursive functions, macros, and
symbolic computation.
Key Features Supporting Declarative Programming:
Recursive Functions: Lisp heavily relies on recursion, a declarative concept where
problems are defined in terms of smaller subproblems, enabling concise and elegant
solutions.Higher-Order Functions: Functions in Lisp can accept other functions as
arguments or return them as results, encouraging abstraction and reuse.Code as Data
(Homoiconicity): Lisp’s unique ability to treat code as data enables advanced
metaprogramming. Developers can create domain-specific languages (DSLs) and
macros to express high-level operations declaratively.Symbolic Computation: Lisp
excels in tasks like symbolic differentiation or logic programming, areas that align
with declarative principles.
Example:
A simple example of declarative programming in Lisp is defining the factorial of a
number:
lisp
Copy code
(defun factorial (n)
(if (= n 0) 1
(* n (factorial (- n 1)))))

17 | P a g e
Declarative
Programming
vs. Imperative
Programming
Declarative and Imperative programming are two fundamentally distinct paradigms.
Declarative programming, as stated before focuses on describing the desired outcome. It
describes “what” needs to be done. Imperative programming focuses on “how” the desired
outcome can be achieved. Below is a table which further proves the differences (Benoit, R.
(October 21, 2022) Declarative vs imperative):

Declarative Imperative
Describes “what” the program does without Describes “how” the program should do
explicitly specifying its control flow. something by explicitly specifying each
instruction step by step.

18 | P a g e
An example which can further explain the differences is baking a chocolate cake:
No table of contents entries found.

Declarative
1. You have to preheat the oven to 180 °C.
2. You have to mix dry ingredients in a bowl.
3. Once dry ingredients are mixed, you have to add wet ingredients to the mixture and
mix together to form the cake batter.
4. Once the oven and batter are ready, you have to put the batter in a pan, then bake it for
30 minutes.
5. Once baked, you have to remove the pan from the oven and let it cool for 10 minutes.
6. Finally, you have to remove the cake from the pan and frost it.
7. Ready? Go!

Imperative
1. First, turn on the oven to preheat it at 180°C.
2. Next, add flour, sugar, cocoa powder, baking soda and salt to a large bowl, then stir
the mixture with a paddle.
3. Then, add milk, vegetable oil, eggs and vanilla extract to the mixture, and mix
together on medium speed until well combined.
4. Distribute the cake batter evenly in a large cake pan, then bake it for approx. 30
minutes.
5. Remove the pan from the oven with a pot holder, let it cool for 10 minutes.
6. Finally, remove the cake from the pan with the tapping method, and frost it evenly
with chocolate frosting.

19 | P a g e
Conclusion
It is important to know about the different types of
programming paradigms and what differentiates them. By
understanding this, a programmer/developer is able to choose
the best approach for a particular problem.

Declarative programming is a unique paradigm that focuses


on specifying what needs to be done rather than how to
achieve the specific goal/aim. This therefore makes it easier to
read, test and maintain with applications in areas like
databases and web development. Languages like SQL,
HTML, Prolog, etc portrays how versatile this paradigm can
be.

Although it has drawbacks like longer debugging, it remains


important in solving modern problems as it is more efficient.

20 | P a g e
References
(Benoit, R. (October 21, 2022) Declarative vs imperative)
(Capital One, (August 5, 2020) The Complete Guide to Declarative Programming)
(StudySmarter UK- Declarative Programming)
(John Samuel, Declarative Programming)
(Pinki, S. (May 1, 2024) Introduction to Programming Paradigm – Unlocking the basics)
(DiscoveringCS – Muhammad Hazeem Mubashar – February 8, 2024)

(Lipovača, M. (2011). Learn you a Haskell for great good!)

(Walmsley, P. (2006). Introduction to XQuery, a tutorial (Updated to align with June 2006 CR
documents).

(Walmsley, P. (2004). Essential XQuery - The XML Query Language.)

21 | P a g e

You might also like