Subject: CS 350 Programming Language Concepts
Prof: Clayton Dsouza
Name: Harsh Pancholi
Stud Id: 200473949
March 08, 2024
Assignment 4
Ans 1)
1. `member`: Checks if an element is a member of a list. Returns the
portion of the list starting with the first occurrence of the element, or `#f` if
the element is not found.
DEFINE (member atm a_list)
(COND
((NULL? a_list) #F)
((EQ? atm (CAR lis)) #T)
((ELSE (member atm (CDR a_list)))
))
2. `equalsimp`: Compares two expressions for structural equality,
disregarding differences in letter case and whitespace.
(DEFINE (equalsimp list1 list2)
(COND
((NULL? list1) (NULL? list2))
((NULL? list2) #F)
((EQ? (CAR list1) (CAR list2))
(equalsimp(CDR list1)(CDR list2)))
(ELSE #F)
))
3. `equal`: Determines if two expressions are structurally equal, taking into
account their complete structure including sub-lists.
(DEFINE (equal list1 list2)
(COND
((NOT (LIST? list1))(EQ? list1 list2))
((NOT (LIST? lis2)) #F)
((NULL? list1) (NULL? list2))
((NULL? list2) #F)
((equal (CAR list1) (CAR list2))
(equal (CDR list1) (CDR list2)))
(ELSE #F)
))
4. `append`: Concatenates lists together, creating a new list that contains
all the elements of the input lists in the order they appear.
(DEFINE (append list1 list2)
(COND
((NULL? list1) list2)
(ELSE (CONS (CAR list1)
(append (CDR list1) list2)))
))
Ans 2)
1. Calculate the cube of a given number using a lambda expression
((lambda (x) (* x x x)) 3)
2. Compute the value of a quadratic equation (ax^2 + bx + c) using a
lambda expression
((lambda (a b c x) (+ (* a x x) (* b x) c)) 2 3 1 5)
3. Calculate the area of a circle given its radius using a lambda expression
(define pi 3.141593)
(define circle-area
(lambda (radius) (* 3.141593 (* radius radius))))
(circle-area 7)
4. Compute the value of a linear equation (mx + c) using a lambda
expression
((lambda (m c x) (+ (* m x) c)) 6 9 5)
5. Create a constant for Euler's number and calculate its square
(define euler 2.71828)
(* euler euler)
Asn 3)
Ans 4)
Here are the key differences between Functional and Imperative
programming languages:
1. Execution Efficiency
- Imperative Languages: Known for efficient execution.
- Functional Languages: Generally less efficient in execution compared to
imperative languages.
2. Semantics
- Imperative Languages: Tend to have complex semantics.
- Functional Languages: Known for having simpler semantics.
3. Syntax
- Imperative Languages: Often characterized by complex syntax.
- Functional Languages: Have simpler syntax compared to imperative
languages.
4. Concurrency
- Imperative Languages: Concurrency is typically designed by the
programmer.
- Functional Languages: Programs can automatically be made
concurrent, suggesting built-in or easier support for concurrency compared
to imperative languages.
Ans 5)
When working with lists or symbols that you do not want evaluated as
function calls or expressions, you must use the Scheme QUOTE function,
sometimes abbreviated as '. Scheme is a Lisp dialect that adheres to the
principle that "code is data and data is code," meaning that data and code
have the same syntax. This explains why this is the case.
An expression created in Scheme is assessed by the compiler or
interpreter based on the language's rules. However, there are times when
you want to think of symbols or lists as literal data rather than as
expressions that must be evaluated. Use the QUOTE function or its
abbreviation "in such cases to prevent evaluation.
For example:
(QUOTE (1 2 3)) ; Equivalent to '(1 2 3), creates a list of three elements
In this example, (1 2 3) is treated as literal data because of the presence of
QUOTE.
Without QUOTE, Scheme would try to evaluate (1 2 3) as a function call,
resulting in an error because there is no function named 1. Using QUOTE
instructs Scheme to treat (1 2 3) as a list literal rather than attempting to
evaluate it.
Ans 6)
1. Goals & Subgoals:
- Goals: Questions or statements you want to prove (like `parent(alice,
bob)`). Can be the main objective or part of a larger goal structure.
- Subgoals: Smaller, more manageable goals created by breaking
down complex goals (e.g., `human(Socrates)` and
`all_humans_are_mortal()` for `mortal(Socrates)`).
2. Resolution:
- The process of finding clauses (facts or rules) in the knowledge base
that match the current goal.
- Involves unification: matching goal predicate and arguments with a
clause's head, potentially binding variables for a match.
- Successful unification creates new subgoals (clause's body) to be
solved. Continues recursively until success or failure.
3. Proof-Tree:
- A visual representation of resolution.
- Root node: main goal. Branches: subgoals created through
unification attempts.
- Successful unifications lead to further branches. Failed unifications
indicate dead ends.
- A complete proof-tree shows all subgoals reaching successful
unification, demonstrating the goal's truthfulness based on the
knowledge base.
4. Binary Resolution Rule:
- A specific type of resolution used for negation handling.
- Unifies two clauses: one with a negated head (prefixed with `not`)
and another with a non-negated head.
- Successful unification creates a new clause with the bodies of both
original clauses joined (logical AND).
- Allows reasoning by contradiction: trying to derive a contradiction with
the knowledge base using negation. If a contradiction is found, the
original fact can be considered true.
Ans 7)
1. Discover Grandparents in the Family:
grandparent(Grandparent, Grandchild) :-
parent(Grandparent, Parent),
parent(Parent, Grandchild).
2. Identify Ella's Parents:
parent(Parent, Ella) :-
parent(Parent, _). /* Search for any parent of Ella */
3. Verify if Jordan qualifies as a Grandparent:
( grandparent(Jordan, Grandchild) ; \ ) /* Try both possibilities */
( parent(Jordan, Grandchild) ). /* Direct parent or grandparent */
This combined query will return true if Jordan is either a grandparent or a
direct parent of someone in the family tree.
Ans 8)
Created With: https://app.diagrams.net/