0% found this document useful (0 votes)
8 views8 pages

PPL Unit 6

The document covers various programming paradigms including Functional Programming, Logical Programming, and LISP. It describes their features, commonly used languages, and provides examples of predicates and recursion in LISP. Additionally, it explains structures and clauses in Prolog, highlighting their syntax and usage.

Uploaded by

gaurav274chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

PPL Unit 6

The document covers various programming paradigms including Functional Programming, Logical Programming, and LISP. It describes their features, commonly used languages, and provides examples of predicates and recursion in LISP. Additionally, it explains structures and clauses in Prolog, highlighting their syntax and usage.

Uploaded by

gaurav274chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PPL UNIT 6

Q.1) Describe Functional Programming. Enlist its features. Also


list the commonly used functional programming languages. [6
Marks]

Answer: Functional Programming (FP) is a programming style where


programs are built using pure functions and immutable data.
It focuses on what to solve, not how to solve.

It avoids changing data and using loops; instead, it uses functions, recursion,
and expressions.

Features of Functional Programming:

Trick: SHIP that sails with PURE functions

Letter Feature
Pure functions (no side effects): Same input always gives same output, no
P
side effects.
U Uses recursion: Replaces loops using function calls.
R Referential transparency
E Expressions over statements
S Stateless (no variable changes): Doesn’t change external state or variables.
H Higher-order functions: Functions that take or return other functions.
I Immutable data: Variables are not changed once set
Pass functions as arguments (First-class): Functions can be passed as
P
arguments or returned.

Commonly Used Functional Programming Languages: Haskell, Lisp, Erlang,


Scala, F#, JavaScript (supports functional programming features), Python
(partially supports functional style).

Q.2) Describe Logical Programming. Enlist its features. Also list


the commonly used Logical programming languages. [6 Marks]

Answer: Logical Programming is a programming style based on formal


logic. In this approach, a program is written as a set of facts and rules, and the
computer uses logical reasoning to solve problems.

It focuses on what is true, not how to do it.

Trick: "Rule Based People Find Smart Answers"

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
Features of Logical Programming:

Word Feature
Rule Rule-based programming: Uses facts and rules to reach conclusions.
Based Backtracking supported: Tries all possible options to find a solution.
People Pattern matching: Matches input data with known rules.
Formal logic-based (declarative style): Describes what to do, not how
Find
to do it.
Smart Symbolic computation: Works with symbols instead of numbers only.
Automatic inference engine: Solves queries using logical inference
Answers
engines.

Common Logical Programming Languages: Prolog (most popular logic


programming language), Datalog, Mercury, Answer Set Programming (ASP),
Absys.

Q.3) Explain the features of LISP programming. [6 Marks]


Answer: LISP stands for LISt Processing.
It is one of the oldest high-level programming languages, mainly used in
Artificial Intelligence (AI) and symbolic computation.

Features of LISP Programming:

🔹 1. Symbolic Expression Based:

 LISP programs are written using S-expressions (symbolic expressions).


 Code and data use the same structure: (operator operand1
operand2 ...)

🔹 2. List-Based Language:

 Everything in LISP is stored and processed as a list.


 Example: (+ 2 3) adds two numbers stored in a list.

🔹 3. Dynamic Typing:

 Variables do not need to be declared with a type.


 Data types are decided at runtime.

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
🔹 4. Garbage Collection:

 LISP has built-in automatic memory management, which removes


unused data.

🔹 5. Recursion Support:

 LISP supports recursion strongly (functions calling themselves).


 Recursion is used instead of loops.

🔹 6. Functional Programming Support:

 Functions in LISP are first-class, meaning they can be passed as


arguments and returned from other functions.

Q.4) What is a predicate in LISP? Explain any 5 predicates with


example. [9 Marks]

Answer: In LISP, a predicate is a function that returns either T (true) or NIL


(false).
Predicates are used to test conditions in programs, especially in decision-
making (if, cond).

Predicate Purpose Example Output


atom Checks if value is an atom (not a list) (atom 'x) T
(atom '(1 2)) NIL
listp Checks if value is a list (listp '(a b c)) T
(listp 'x) NIL
numberp Checks if value is a number (numberp 5) T
(numberp 'a) NIL
symbolp Checks if value is a symbol (symbolp 'a) T
(symbolp 42) NIL
null Checks if value is NIL or empty list (null '()) T
(null '(a)) NIL
equal Checks if two values are equal (equal 4 4) T
(equal '(1 2) '(1
T
2))
Checks if two atoms are exactly the
eq (eq 'a 'a) T
same

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
Predicate Purpose Example Output
(eq 'a 'b) NIL
zerop Checks if a number is zero (zerop 0) T
(zerop 5) NIL
evenp Checks if a number is even (evenp 4) T
(evenp 5) NIL
oddp Checks if a number is odd (oddp 3) T
(oddp 8) NIL
>, <, =, >=, Comparison predicates (greater,
(< 3 5) T
<= equal, etc.)

Q.5) What is Recursion? Write a LISP function to calculate


power of a number using recursion and iteration. [9 Marks]

Answer: Recursion is a programming technique where a function calls itself


to solve smaller versions of the same problem.
It continues until a base case is met.

1. Power Function using Recursion

(defun power-rec (base exp)


(if (= exp 0)
1
(* base (power-rec base (- exp 1)))))

Example: (power-rec 2 3) returns 8

2. Power Function using Iteration

(defun power-iter (base exp)


(let ((result 1))
(dotimes (i exp result)
(setf result (* result base)))))

📌 Example: (power-iter 2 3) returns 8

Explanation:

 Recursive version: multiplies base with itself exp times using function
calls.

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
 Iterative version: uses a loop (dotimes) and a variable to multiply
repeatedly.

Q.6) Write a LISP program to find the factorial of n numbers


using recursion. [6 Marks]

Answer: LISP Code (Recursive Factorial):


(defun fact (n)
(if (= n 0)
1
(* n (fact (- n 1)))))

Example Call:

(print (fact 5)) ; Output: 120

Explanation (in simple words):

 The function fact takes a number n.


 If n is 0, it returns 1 (base case).
 Else it multiplies n with fact(n - 1) (recursive step).

Q.7) Explain the following functions with suitable examples:


CAR(), CDR(), FIRST(). [6 Marks]

Answer:
1. CAR(): car is a LISP function that returns the first element of a list.

📌 Syntax: (car list)

📌 Example: (car '(a b c)) ; Output: a

2. CDR(): cdr returns the rest of the list after removing the first element.

Syntax: (cdr list)

Example: (cdr '(a b c)) ; Output: (b c)

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
3. FIRST()

 first is similar to car. It also returns the first element of a list.


 It is part of Common LISP and often used for readability.

Example:

(first '(x y z)) ; Output: x

Q.8)

Q.*

Answer:

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
Q.9) Explain with example how to use structures in Prolog. [9 M]

Answer: In Prolog, a structure is a compound term used to group related


data together.
It is like a record or object that has a name (functor) and one or more
arguments.

Syntax of Structure : functor(argument1, argument2, ..., argumentN).

The functor is the structure name, and arguments can be constants, variables,
or other structures.

Example 1: Representing a Student

student(john, 22, computer_engineering).


student(rahul, 20, electronics).

 student is the structure name (functor)


 john, 22, computer_engineering are the arguments

Example 2: Structure Inside a Structure:


address(city(pune), state(maharashtra)).

 address is the main structure


 city(pune) and state(maharashtra) are sub-structures

Q.10) What is a clause in Prolog? Explain types of clauses with


example. [9 Marks]
Answer: In Prolog, a clause is a basic building block of a program.
A clause represents a fact, rule, or goal in Prolog.

It is written in the form of:

head :- body.

Or simply:

fact.

By: Aaryan Waghmare and Chat GPT


PPL UNIT 6
Types of Clauses in Prolog:

Prolog has 3 main types of clauses:

1. Fact (Unit Clause)

 Represents a known truth.


 It has no condition or body.

Example:

sun_rises_in(east).
likes(john, pizza).

2. Rule (Implication Clause)

 Represents a condition-based logic.


 Syntax: Head :- Body.
(Means: Head is true if Body is true)

Example:

happy(X) :- rich(X).

Means: X is happy if X is rich.

3. Goal (Query Clause)

 Represents a question asked to the system.


 Used by the user to find answers based on facts and rules.

Example:

?- likes(john, pizza).

Output: true

By: Aaryan Waghmare and Chat GPT

You might also like