PPL 6
PPL 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.
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.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.