0% found this document useful (0 votes)
47 views5 pages

PPL 6

The document contains a PPL assignment by Nikhil Nalawade, detailing sequences of CAR and CDR operations in Lisp to extract the atom 'pear' from various list structures. It also evaluates specific LISP expressions, defines recursive and iterative functions for calculating power, and explains several number predicates with examples. Additionally, it describes functional programming, its key features, advantages, and lists commonly used functional programming languages.

Uploaded by

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

PPL 6

The document contains a PPL assignment by Nikhil Nalawade, detailing sequences of CAR and CDR operations in Lisp to extract the atom 'pear' from various list structures. It also evaluates specific LISP expressions, defines recursive and iterative functions for calculating power, and explains several number predicates with examples. Additionally, it describes functional programming, its key features, advantages, and lists commonly used functional programming languages.

Uploaded by

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

PPL Assignment - 6

Nikhil Nalawade
SE – A – 77

Q.1) Write sequences of CAR’s and CDR’s that will pick the atom pear out of the
following expression:
i) (apple orange pear grapes)
ii) ((apple orange) (pear grapes))
iii) (((apple)(orange) (pear) (grapes)))
Ans - To extract the atom pear using sequences of CAR and CDR in Lisp, we navigate through the
list structure carefully.

1. Expression: (apple orange pear grapes)


To get pear:
(car (cdr (cdr '(app))))
Explanation: (apple orange pear grapes)
CDR once: (orange pear grapes)
CDR twice: (pear grapes)
CDR on (pear grapes) : pear

2. Expression: ((apple orange) (pear grapes))


To get pear:
(car (cdr '((apple orange) (pear grapes))))
Explanation:
CDR once: ((pear grapes))
CAR on ((pear grapes)): (pear grapes)
CAR again : pear

3. Expression: (((apple)(orange) (pear)(grapes)


To get pear:
(car (cdr (cdr '(((apple)(orange) (pear) (grapes))))))
Explanation:
CDR once: ((orange)(pear)(grapes))
CDR twice: ((pear) (grapes))
CAR on ((pear)(grapes)) : (pear)
CAR again: (pear)

Each sequence efficiently retrieves pear from the nested lists using recursive CDR
operations to traverse and CAR to extract the desired element.
Q.2 Evaluate the following forms of LISP.
i) (car (cdr’(1 2 3 4 5 )))
ii) (car (cdr’ (a (b c) d e)))
iii) (car (cdr (cdr’(1 2 3 4 5 6 7 8))))
Ans –
Let’s evaluate each LISP expression step by step:
i) (car (cdr '(1 2 3 4 5)))
 **cdr of '(1 2 3 4 5)**: This removes the first element of the list, resulting in (2 3 4
5)`.
 car of (2 3 4 5): This extracts the first element, which is 2.
Result: 2
ii) (car (cdr '(a (b c) d e)))
 **cdr of '(a (b c) d e)**: This removes the first element a, resulting in ((b c) d e)`.
 car of ((b c) d e): This extracts the first element, which is (b c).
Result: (b c)
iii) (car (cdr (cdr '(1 2 3 4 5 6 7 8))))
 **First cdr of '(1 2 3 4 5 6 7 8)**: Removes the first element, resulting in (2 3 4 5 6 7
8)`.
 Second cdr of (2 3 4 5 6 7 8): Removes the first element again, resulting in (3 4 5 6 7
8).
 car of (3 4 5 6 7 8): Extracts the first element, which is 3.
Result: 3

Q.3 Write a lisp function to calculate power of a number using recursion and iteration.
Ans - Using Recursion
(defun power-recursive (base exp)
(if (zerop exp) ; Base case: if exponent is zero
1
(* base (power-recursive base (- exp 1)))))
If the exponent is 0, the function returns 1.
Otherwise, it multiplies the base by the result of the function call with the exponent
decremented by 1
Using Iteration
(defun power-iterative (base exp)
(let ((result 1)) ; Initialize result to 1
(dotimes (i exp result) ; Loop `exp` times
(setq result (* result base)))))
The dotimes construct iterates exp times.
During each iteration, the result is multiplied by the base.

Q.4 Explain the following number predicates using suitable example.


i) NUMBERP
ii) ZEROP
iii) PLUSP
iv) EVENP
v) ODDP
Ans –
i) NUMBERP
 Description: This predicate checks whether the given argument is a number.
 Example:
(numberp 42) ; Returns T
(numberp 'abc) ; Returns NIL
Explanation: 42 is a number, so it returns T. 'abc is not
ii) ZEROP
 Description: This predicate checks whether the given argument is zero.
 Example:
(zerop 0) ; Returns T
(zerop 3) ; Returns NIL
Explanation: If the number is 0, it returns T. Otherwise, it returns NIL.
iii) PLUSP
 Description: This predicate checks whether the given number is positive.
 Example:
(plusp 10) ; Returns T
(plusp -5) ; Returns NIL
Explanation: 10 is a positive number, so it returns T. -5 is negative, so it returns NIL.
iv) EVENP
 Description: This predicate checks whether the given number is even.
 Example:
(evenp 4) ; Returns T
(evenp 7) ; Returns NIL
Explanation: 4 is even, so it returns T. 7 is odd, so it returns NIL.
v) ODDP
 Description: This predicate checks whether the given number is odd.
 Example:
(oddp 5) ; Returns T
(oddp 8) ; Returns NIL
Explanation: 5 is odd, so it returns T. 8 is even, so it returns NIL.

Q.5 Describe Functional Programming. Enlist its features. Also list the commonly used
functional programming languages.
Ans –
Functional Programming: An Overview
Functional programming is a paradigm designed for symbolic computation and list
processing. It focuses on mathematical functions and avoids changing states and mutable
data. Instead, computations are expressed in terms of functions.
Key Features of Functional Programming
1. Immutable Data: Data objects cannot be modified after creation, ensuring
consistency and reducing bugs.
2. Higher-Order Functions: Functions can take other functions as arguments or return
them, enabling flexible computation.
3. Lazy Evaluation: Expressions are not evaluated until their values are required,
optimizing performance.
4. Recursion over Iteration: Instead of loops, recursion is used for repetitive tasks.
5. Declarative Nature: Focuses on what to compute rather than how to compute it.
6. Statelessness: No reliance on global state or side effects, leading to cleaner and
testable code.
7. Nested Functions: Allows defining functions within functions for better modularity.
Advantages
 Bug-Free Code: Eliminates side effects, resulting in error-free programs.
 Concurrency-Friendly: Immutable state supports parallel execution without
conflicts.
 Efficiency: Independent units of computation can run concurrently.
 Abstraction & Encapsulation: Supports OOP-like concepts for better organization.
Commonly Used Functional Programming Languages
1. Lisp: Known for its symbolic computation and AI applications.
2. Haskell: A pure functional language with strong typing and lazy evaluation.
3. Erlang: Designed for concurrent and distributed systems.
4. Clojure: A modern Lisp dialect running on the JVM.
5. Python: Supports functional programming alongside imperative styles.

You might also like