0% found this document useful (0 votes)
27 views12 pages

PPL Unit 6

The document explains predicates in LISP, which are functions that return true or false values, and provides examples of five common predicates: ATOM, LISTP, NULL, EQUAL, and NUMBERP. It also discusses features of LISP programming, including symbolic expression handling and dynamic typing, and describes logical programming and functional programming, highlighting their features and commonly used languages. Additionally, it evaluates specific LISP forms and demonstrates how to extract elements from s-expressions using CAR and CDR.

Uploaded by

Vaibhav Joshi
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)
27 views12 pages

PPL Unit 6

The document explains predicates in LISP, which are functions that return true or false values, and provides examples of five common predicates: ATOM, LISTP, NULL, EQUAL, and NUMBERP. It also discusses features of LISP programming, including symbolic expression handling and dynamic typing, and describes logical programming and functional programming, highlighting their features and commonly used languages. Additionally, it evaluates specific LISP forms and demonstrates how to extract elements from s-expressions using CAR and CDR.

Uploaded by

Vaibhav Joshi
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/ 12

Question:

What is a predicate in LISP? Explain any 5 predicates with examples.

Answer 1: In Simple English (Theory + Code)

What is a Predicate in LISP?

• In LISP, a predicate is a function that returns either T (true) or NIL (false).

• Predicates are used to test conditions.

All predicates in LISP return Boolean values.

5 Common Predicates in LISP with Examples

1. ATOM

Checks if an expression is an atom (not a list).

lisp

CopyEdit

(ATOM 'A) ;→T

(ATOM '(A B)) ; → NIL

2. LISTP

Checks if the given value is a list.

lisp

CopyEdit

(LISTP '(1 2 3)) ; → T

(LISTP 'A) ; → NIL

3. NULL

Checks if the value is NIL (empty list).

lisp
CopyEdit

(NULL NIL) ;→T

(NULL '(A)) ; → NIL

4. EQUAL

Checks if two expressions are equal in content.

lisp

CopyEdit

(EQUAL 3 3) ;→T

(EQUAL '(A B) '(A B)) ; → T

(EQUAL 3 4) ; → NIL

5. NUMBERP

Checks if the value is a number.

lisp

CopyEdit

(NUMBERP 10) ; → T

(NUMBERP 'A) ; → NIL

Question:
Explain the features of LISP programming.

Answer 1: In Simple English (Theory Points)

LISP (LISt Processing) is a programming language mainly used for Artificial Intelligence
(AI) and symbolic computation.

Features of LISP Programming:

1. Symbolic Expression Handling


➤ LISP works with symbols and expressions (like (+ 2 3)).
2. Code as Data (Homoiconicity)
➤ LISP programs are written in the same format as data — both are lists.

3. Dynamic Typing
➤ No need to declare variable types in advance.

4. Garbage Collection
➤ LISP automatically manages memory, removing unused data.

5. Recursion Support
➤ Functions can call themselves. Very useful for AI logic.

6. Interactive Language
➤ You can test and run code line-by-line in the LISP interpreter.

7. List-based Data Structure


➤ Everything in LISP is made from lists. Lists are the main building blocks.

8. Function-Oriented Programming
➤ LISP supports defining and using functions everywhere.

ChatGPT said:

Question:
Explain the following Equality predicates using a suitable example.
i) EQUAL
ii) EQ
iii) EQL
iv) =

Answer 1: In Simple English with Examples

LISP has different equality predicates to compare values. Each has a different use based on
what you are comparing (numbers, symbols, strings, etc.).

i) EQUAL

• Used to compare two lists, strings, or complex structures.

• It checks structural equality.

lisp

CopyEdit
(equal '(1 2 3) '(1 2 3)) ; Returns T (true)

(equal "hello" "hello") ; Returns T

ii) EQ

• Used to compare if both are exactly the same object (memory reference).

• Fastest but limited use.

• Does not work well with numbers or strings.

lisp

CopyEdit

(eq 'a 'a) ; Returns T

(eq 100 100) ; Might return NIL (depends on implementation)

(eq "hi" "hi") ; Returns NIL (different string objects)

iii) EQL

• Like EQ, but also works for numbers of the same type and value.

lisp

CopyEdit

(eql 5 5) ; Returns T

(eql 5 5.0) ; Returns NIL (int vs float)

(eql 'a 'a) ; Returns T

iv) = (Equal-to)

• Only used for comparing numbers (integers or floats).

• Returns true if both numbers are equal.

lisp

CopyEdit

(= 10 10) ; Returns T

(= 10 10.0) ; Returns T
(= 10 20) ; Returns NIL

ChatGPT said:

Question:
Explain the following number predicates using a suitable example. [5]
i) ZEROP
ii) PLUSP
iii) EVENP
iv) ODDP

Answer 1: In Simple English with Examples

In LISP, number predicates are used to check number properties like zero, positive, even, or
odd.

i) ZEROP

Checks if a number is zero.


Returns T if number is 0.

lisp

CopyEdit

(zerop 0) ;T

(zerop 5) ; NIL

ii) PLUSP

Checks if a number is positive (> 0).


Returns T if number is greater than 0.

lisp

CopyEdit

(plusp 10) ; T

(plusp -3) ; NIL

iii) EVENP
Checks if a number is even.
Returns T if number is divisible by 2.

lisp

CopyEdit

(evenp 4) ;T

(evenp 7) ; NIL

iv) ODDP

Checks if a number is odd.


Returns T if number is not divisible by 2.

lisp

CopyEdit

(oddp 5) ;T

(oddp 8) ; NIL

Question:
Explain the following functions with suitable examples:
i) CAR()
ii) CDR()
iii) FIRST()

Answer 1: In Simple English with Examples

In LISP, these functions are used to access elements of a list.

i) CAR()

CAR returns the first element of a list.

lisp

CopyEdit

(car '(10 20 30))

; Output: 10
ii) CDR()

CDR returns the rest of the list (excluding the first element).

lisp

CopyEdit

(cdr '(10 20 30))

; Output: (20 30)

iii) FIRST()

FIRST is same as CAR. It returns the first element of the list.


It is more readable and modern than CAR.

lisp

CopyEdit

(first '(a b c))

; Output: a

ChatGPT said:

Question:

Describe Logical Programming. Enlist its features. Also, list the commonly used Logical
Programming languages.

Answer 1: In Simple English (Theory for Exam)

What is Logical Programming?

Logical Programming is a programming paradigm that is based on formal logic.


Instead of telling the computer how to do something (like in procedural languages), you tell
it what you want to achieve using rules and facts.

• It uses declarations (facts and rules) instead of step-by-step instructions.

• The most common language is Prolog (Programming in Logic).

Features of Logical Programming:


1. Declarative Style
(You describe what you want, not how to do it.)

2. Based on Formal Logic


(Uses logic concepts like predicates, clauses, unification.)

3. Rule-based Execution
(Programs consist of rules and facts, queries give output.)

4. Automatic Backtracking
(Tries different solutions automatically if one fails.)

5. Built-in Pattern Matching


(Easily matches values, variables, and structures.)

6. Non-Procedural
(Focus is on logic and relationships rather than steps.)

Commonly Used Logical Programming Languages:

• Prolog – Most popular

• Mercury – Advanced logic programming language

• Datalog – Used in database queries

• Answer Set Programming (ASP) – Used in AI

• Logtalk – Object-oriented extension of Prolo

Question:

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))))

Answer 1: In Simple English (with explanation)

First understand basic LISP functions:

• car → Returns the first element of a list

• cdr → Returns the rest of the list (excluding the first element)

• ' → Quote means the list should be taken as it is, not evaluated.
i) (car (cdr '(1 2 3 4 5)))

Step-by-step:

1. (cdr '(1 2 3 4 5)) → gives (2 3 4 5)

2. car (2 3 4 5) → gives 2

Answer: 2

ii) (car (cdr '(a (b c) d e)))

Step-by-step:

1. (cdr '(a (b c) d e)) → gives ((b c) d e)

2. car ((b c) d e) → gives (b c) (first element of the remaining list)

Answer: (b c)

iii) (car (cdr (cdr '(1 2 3 4 5 6 7 8))))

Step-by-step:

1. (cdr '(1 2 3 4 5 6 7 8)) → (2 3 4 5 6 7 8)

2. (cdr (2 3 4 5 6 7 8)) → (3 4 5 6 7 8)

3. car (3 4 5 6 7 8) → gives 3

Answer: 3

Question:

Describe Functional Programming. Enlist its features. Also list the commonly used
functional programming languages.

Answer 1: In Simple English

What is Functional Programming?

Functional Programming is a programming style where functions are used as the main
building blocks. It treats computation as the evaluation of mathematical functions and
avoids changing data (no variables are modified).
Key Features of Functional Programming:

1. Pure Functions

o Functions return the same output for the same input without side effects.
(E.g., no file writing, no variable updating inside the function)

2. Immutability

o Once a value is set, it cannot be changed.


(No x = x + 1 kind of operations.)

3. First-Class and Higher-Order Functions

o Functions can be passed as arguments, returned from other functions, or


stored in variables.

4. Recursion

o Loops are avoided. Instead, recursion is used to perform repetitive tasks.

5. Lazy Evaluation

o Values are computed only when needed.


(Improves performance.)

6. Referential Transparency

o An expression always gives the same result without changing the program
state.

7. Modularity

o Code is divided into small, reusable functions.

Commonly Used Functional Programming Languages:

Language Notes

LISP One of the oldest functional languages.

Haskell Pure functional programming.

Scheme Simplified version of LISP.

Scala Combines OOP and functional features.


Language Notes

Erlang Used in telecom and fault-tolerant systems.

Clojure Modern LISP on the JVM.

F# Functional-first language in .NET.

JavaScript Not purely functional but supports functional features

Q Write sequences of CAR’s and CDR’s that will pick the atom pear our of the
following s-expression : [6] i) ii) iii) c) (apple orange pear grapes) ((apple
orange) (pear grapes)) (((apple)(orange) (pear) (grapes)))
ChatGPT said:

Question:

Write sequences of CAR's and CDR's that will pick the atom pear out of the following s-
expressions:

Answer in Simple Form

To extract pear, we use combinations of car and cdr in LISP.


Let’s go one-by-one.

i) (apple orange pear grapes)

This is a simple flat list:

lisp

CopyEdit

(car (cdr (cdr '(apple orange pear grapes))))

Explanation:

• (cdr '(apple orange pear grapes)) → (orange pear grapes)

• cdr again → (pear grapes)

• car of that → pear

ii) ((apple orange) (pear grapes))


This is a list of two lists.

lisp

CopyEdit

(car (cdr (car (cdr '((apple orange) (pear grapes))))))

OR simply:

lisp

CopyEdit

(car (car (cdr '((apple orange) (pear grapes)))))

Explanation:

• cdr → ((pear grapes))

• car → (pear grapes)

• car → pear

iii) (((apple)(orange)(pear)(grapes)))

This is a deeply nested list.

lisp

CopyEdit

(car (car (cdr (cdr (car '(((apple)(orange)(pear)(grapes)))))))

Breakdown:

• car → ((apple)(orange)(pear)(grapes))

• cdr → ((orange)(pear)(grapes))

• cdr again → ((pear)(grapes))

• car → (pear)

• car → pear

You might also like