Professional Documents
Culture Documents
#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
(format t "The sum of ~a and ~a is ~a" number1 number2 result) ; Print the result
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
ue
while (a + b) <= x do
begin
next := a + b;
Write(next, ' ');
M
a := b;
b := next;
end;
16:
(defvar result '())
Ab
17:
(defun prefix (n lst)
(if (or (<= n 0) (null lst))
nil
(cons (car lst) (prefix (- n 1) (cdr lst)))))
id
lst
(suffix (- n 1) (cdr lst))))
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
((> n 1)
(cons (car lst) (insert-nth item (- n 1) (cdr lst))))
(t
(cons item lst))))
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)))))))))
id
(setq updated-list (removeContig my-list))
(print updated-list)
ue
21:
(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.
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).
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:
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:
● 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:
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.