Write short notes: i) Functional programming ii) Resolution Vs Unification
Q.) Write short notes: i) Functional programming ii) Resolution Vs Unification
Subject: Principles of Programming LanguagesFunctional Programming
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative type of programming based on the concept of mathematical functions.
Key Concepts of Functional Programming
First-class and Higher-order functions
In functional programming, functions are first-class citizens. This means that functions can be passed as arguments to other functions, returned as values from other functions, and assigned to variables. Higher-order functions are a direct consequence of this property. These are functions that take other functions as arguments or return functions as their results.
Pure Functions
Pure functions are another key concept in functional programming. These are functions where the return value is only determined by its input values, without observable side effects. This means that given the same input, the function will always return the same output.
Recursion
Recursion is a method of solving problems in functional programming where the solution depends on solutions to smaller instances of the same problem. Instead of using loops, functional programming relies heavily on recursion to repeat operations.
Immutable data
In functional programming, data is immutable, meaning it cannot be changed after it's created. This avoids issues with data changes that can lead to bugs and makes the code safer and easier to understand.
Advantages and Disadvantages of Functional Programming
Functional programming offers several advantages such as simplicity, predictability, and ease of testing and debugging due to its pure functions and immutable data. However, it also has some disadvantages such as difficulty in understanding for those used to imperative programming styles and less efficient memory use due to the creation of new data structures instead of modifying existing ones.
Examples of Functional Programming
Functional programming languages include Haskell, Lisp, and Scala. Here is a simple example in Haskell:
-- Function to calculate factorial of a number
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
Resolution Vs Unification
Resolution and unification are techniques used in logic programming and automated theorem proving.
Resolution
Resolution is a rule of inference used for propositional logic and predicate logic. It produces a new clause when two clauses have complementary literals. The resolution rule is used in a search algorithm to find a contradiction in a set of clauses and thus prove a theorem.
Unification
Unification, on the other hand, is an algorithm to solve equations of symbolic expressions. It is used to find a substitution that makes two symbolic expressions equal. Unification is a fundamental part of logic programming and is used in the process of executing a program.
Differences between Resolution and Unification
The main differences between resolution and unification can be summarized as follows:
Resolution | Unification |
---|---|
Rule of inference | Algorithm |
Used to derive a contradiction from a set of clauses | Used to find a substitution that makes two symbolic expressions equal |
Examples of Resolution and Unification
In logic programming or automated theorem proving, resolution and unification are used together to find a solution. For example, in Prolog, a logic programming language, the execution of a program is done by trying to unify a goal with the head of a clause and then using resolution to prove the body of the clause.
Diagram: Not necessary for this question.
Summary
Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is based on the concept of mathematical functions and key concepts include first-class and higher-order functions, pure functions, recursion, and immutable data. Advantages include simplicity and predictability, while disadvantages include difficulty in understanding and less efficient memory use. Examples of functional programming languages include Haskell, Lisp, and Scala.
Resolution and unification are techniques used in logic programming and automated theorem proving. Resolution is a rule of inference used to find a contradiction in a set of clauses and prove a theorem. Unification is an algorithm used to find a substitution that makes two symbolic expressions equal. The main differences between resolution and unification are that resolution is a rule of inference while unification is an algorithm.
Analogy
Functional programming is like following a recipe where you have a set of ingredients (input) and a set of instructions (functions) to create a dish (output). Resolution is like finding a contradiction in a set of clues to solve a mystery, while unification is like finding a common solution that satisfies multiple equations.
Quizzes
- A programming paradigm based on mathematical functions
- A programming paradigm that allows changing-state and mutable data
- A programming paradigm that focuses on loops and iterations
- A programming paradigm that uses object-oriented concepts