You are on page 1of 7

Factorial:

#include <stdio.h>

int factorial(int n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}

id
}

int main() {
int num;

ue
printf("Enter a number: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
M
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result);
}

return 0;
l
}
du

Lisp add two number:

(defun add-two-numbers (num1 num2)


(+ num1 num2))
Ab

(setq number1 5) ; Assign a value to number1


(setq number2 7) ; Assign a value to number2

(setq result (add-two-numbers number1 number2)) ; Call the function

(format t "The sum of ~a and ~a is ~a" number1 number2 result) ; Print the result

The sum of 5 and 7 is 12


Fibonacci pascal:

program FibonacciSeries;

var
x, a, b, next: Integer;

begin
Write('Enter the value of x: ');
Readln(x);

id
a := 0; // First Fibonacci number
b := 1; // Second Fibonacci number

Write('Fibonacci Series: 0 1 ');

ue
while (a + b) <= x do
begin
next := a + b;
Write(next, ' ');
M
a := b;
b := next;
end;

Writeln; // Start a new line


l
end.
du

16:
(defvar result '())
Ab

(defun myinter (set1 set2)


(if (null set1)
nil
(if (member (car set1) set2)
(progn
(setq result (cons (car set1) result))
(myinter (cdr set1) set2))
(myinter (cdr set1) set2)))) ; Recursive call without adding to the result

(setq result '()) ; Clearing variable


(myinter '(1 2 3 4 5) '(3 4 5 6 7))
(print result) ; Printing the result

17:
(defun prefix (n lst)
(if (or (<= n 0) (null lst))
nil
(cons (car lst) (prefix (- n 1) (cdr lst)))))

(defun suffix (n lst)


(if (or (<= n 0) (null lst))

id
lst
(suffix (- n 1) (cdr lst))))

(defun remove-nth (n list)

ue
(if (null list)
nil
(if (> n 0)
(cons (car list) (remove-nth (- n 1) (cdr list)))
(remove-nth n (cdr list)))))
M
(setq my-list '((1 2 (3 4) 5) 6 7 (8 (9 10)))
(setq updated-list (remove-nth 3 my-list))
(print updated-list)
18:
Same as 17
l
19:
du

(defun insert-nth (item n lst)


(cond
((null lst) nil)
((and (listp (car lst)) (> n 0))
(cons (insert-nth item n (car lst)) (insert-nth item n (cdr lst))))
Ab

((> n 1)
(cons (car lst) (insert-nth item (- n 1) (cdr lst))))
(t
(cons item lst))))

(setq my-list '((1 2 (3 4) 5) 6 7 (8 (9 10)))


(setq updated-list (insert-nth 'X 3 my-list))
(print updated-list)

20:
(defun removeContig (theList)
(if (null theList)
nil
(cond
((atom theList) (cons (car theList) (removeContig (cdr theList))))
(t
(let ((first-elem (car theList)))
(if (null first-elem)
(removeContig (cdr theList)) ; Skip null element
(cons (removeContig first-elem) (removeContig (cdr theList)))))))))

(setq my-list '((1 2 2 (3 3 3) 4 4 4) 5 (5 5) 6 6 6 7))

id
(setq updated-list (removeContig my-list))
(print updated-list)

ue
21:

Let's go through each of the given forms:

(cons c (car a)): This will try to create a new list by consing c (undefined) with the result of (car
M
a). If a is the same as it was defined, i.e., (ab), this will result in (cons c (car (ab))). However, this
is not a valid operation as c is undefined.

(cons e b): This will try to create a new list by consing e and b. e is ((lambda (x) (list x)) d), which
means e is a list containing d (which is equivalent to c). b is (cdr a), so (cons e b) will result in
((lambda (x) (list x)) d . (cdr a)).
l
du

(eval a): This will attempt to evaluate a. However, a is now set to (ab). This will result in trying to
evaluate (ab), and it will call the function ab with no arguments. If ab is not a defined function, it
will result in an error.

(let ((ab) (y a)) (append a y)): This code uses let to create a local variable y and tries to append
Ab

a and y. However, (ab) will try to call a function named ab (which is not defined here) and not
create a variable. This will result in an error.

(multiple-value-list (a a)): This will call the function a twice with the same argument a. The first
(a) will return (list a a). The second (a) will just return a. So, (multiple-value-list (a a)) will result
in the list (a a).

(bc): This will try to call a function named bc, but no such function is defined in the provided
code. This will result in an error.
22:
Static (Lexical) Scoping:
In static scoping, the binding of a variable is determined by its location in the source code.
When a function is defined, it captures the environment where it's defined.

(fl 6): fl is defined as (defun fl (vl) (12 v1)). Here, the variable vl is local to the fl function. When fl
is called with 6, it uses vl from its parameter, which is 6, and then tries to access v1 (which is
undefined). This will result in an error due to the undefined variable v1.

(setq vl 10): This sets the global variable vl to 10.

id
The environment inside the function f2 under static scoping will look like:

v2: A local variable with the value passed when the function is called.
vl: This variable would capture the global variable vl (since it's not defined locally within f2).

ue
Dynamic Scoping:
In dynamic scoping, variable binding is determined by the current state of the program,
specifically the calling sequence.

(fl 6): In dynamic scoping, the function fl would use the most recent (dynamic) binding of vl. If we
M
have previously set vl to 10 with (setq vl 10), then this call would use that value. So, (fl 6) would
result in (12 10).

(setq v1 10): This sets the global variable v1 to 10.

Under dynamic scoping, the environment inside the function f2 would look like:
l
du

v2: A local variable with the value passed when the function is called.
vl: This variable would capture the dynamic binding of vl from the most recent call sequence.
Ab

23:
A:
(defun nodups (alist)
(cond ((null alist) nil)
((null (cdr alist)) alist)
((equal (car alist) (cadr alist)) (nodups (cdr alist)))
(t (cons (car alist) (nodups (cdr alist))))))
This function uses the following logic:

● If the list is empty, return an empty list.


● If there's only one item in the list, return the list itself.
● If the first two items are equal, skip the first item and recursively call nodups on the rest
of the list.
● If the first two items are not equal, keep the first item and recursively call nodups on the
rest of the list.

B:
(defun dups (alist n)
(cond
((null alist) nil)
((null (cdr alist)) (dups-element (car alist) n))

id
(t (append (dups-element (car alist) n) (dups (cdr alist) n)))
)
)

ue
(defun dups-element (element n)
(if (null element)
nil
(append (list element) (dups-element element (- n 1)))
)
)
M
This function works as follows:

● The dups function takes a list alist and an integer n as arguments.


● If alist is empty, it returns an empty list.
● If there's only one item in alist, it duplicates that item n times using the dups-element
l
function.
du

● If there are multiple items in alist, it processes the first item using dups-element, and
then recursively processes the rest of the list, appending the results together.
Ab

24:
(defun dup (lst)
(cond
((null lst) nil) ; If the list is empty, return nil.
((atom lst) nil) ; If the current element is an atom, return nil.
((equal (car lst) (cadr lst)) t) ; If the first two elements are equal, return true.
(t (dup (cdr lst))) ; Recursively check the rest of the list.
)
)
This function works as follows:
● If the input list lst is empty or if the current element is an atom, the function returns nil
because it cannot contain two successive equal elements.

● If the first two elements (car lst) and (cadr lst) are equal, the function returns t, indicating
that it has found two successive equal elements.

● If the above conditions are not met, the function recursively checks the rest of the list
using (cdr lst).

id
25 :

ue
The Lisp function mystery takes a single argument s and appears to be recursively processing a
list. Let's break down:

(defun mystery (s)


(cond
M
((null s) 1) ; If s is null (an empty list), return 1.
((atom s) 0) ; If s is an atom (not a list), return 0.
(t (max (+ (mystery (first s)) 1) (mystery (rest s))))
)
)
l
● If s is null (an empty list), the function returns 1. This serves as the base case for the
du

recursive calls.

● If s is an atom (not a list), the function returns 0. This handles the case where s is an
atom and not a list.

Ab

● If neither of the above conditions is met, it implies that s is a non-empty list. In this case,
the function takes the maximum of two values:
● The first value is the result of a recursive call to mystery with the first element of the list
(first s) incremented by 1.
● The second value is the result of a recursive call to mystery with the rest of the list (rest
s).
● So, in general, the mystery function seems to be calculating the maximum depth or level
of nested lists
● For example, if you call (mystery '(a (b (c) d)), it would return 3 because it's a list with
three levels of nesting, and it counts the top-level elements.

You might also like