Question

In: Computer Science

Given the following Scheme definition: (define x '(define (fac n) (if (= n 0) 1 (*...

Given the following Scheme definition:

(define x '(define (fac n)
              (if (= n 0) 1
                  (* n (fac (- n 1))))))

(This does not define the factorial function fac, but the variable x.)

Write Scheme expressions in terms of x that would have the effect of extracting the following expressions:

  • (fac n)
  • 0
  • (- n 1)
  • The second occurrence of fac.
  • The last occurrence of n.

E.g., the expression (car x) would extract define.

Solutions

Expert Solution

Given below statements are equivalent.

  • (car (cdr x) is same as (cadr x)
  • (car (cdr (cdr x))) is same as (caddr x)
  • (cdr (cdr x)) is same as (cddr x)
  • (cdr (cdr (cdr x) is same as (cdddr x)

Below are the scheme expressions and their corresponding output :

1)

(cadr x) ==> (fac n)

2)

(caddr (car (cdr (car (cddr x))))) ==> 0

3)

(cadr (caddr (car (cdddr (car (cddr x)))))) ==> (- n 1)

4)

(car (caddr (car (cdddr (car (cddr x)))))) ==> fac (second occurance)

5)

(cadr (cadr (caddr (car (cdddr (car (cddr x))))))) ==> n (last occurance)

Snapshot showing step by step extractions of scheme expressions: (Tick marked are required ones)


Related Solutions

Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
Given a difference equation x[n+2] + 5x[n+1]+6x[n]=n with start values x[0]= 0 and x[1]=0
Consider the following Scheme function foo: (define (foo x) (cond ((null? x) 0) ((not (list? (car...
Consider the following Scheme function foo: (define (foo x) (cond ((null? x) 0) ((not (list? (car x))) (cond ((eq? x '()) (foo (car x))) (else (+ 1 (foo (cdr x)))))) (else (+ (foo (car x)) (foo (cdr x)))))) (8 pts) Explain what the function computes and how. Don’t just restate the function definition in English — explain the algorithm. Hint: not all the code in this function does something useful. (2 pts) Show the result of executing the expression (foo...
Given a sequence x(n) for 0 ≤ n ≤ 3, where x(0)=4, x(1)=3, x(2)=2, and x(3)=1,...
Given a sequence x(n) for 0 ≤ n ≤ 3, where x(0)=4, x(1)=3, x(2)=2, and x(3)=1, evaluate your DFT X(k)
1) Define a sequence of polynomials H n (x ) by H 0 (x )=1, H...
1) Define a sequence of polynomials H n (x ) by H 0 (x )=1, H 1 (x )=2 x , and for n>1 by H n+1 (x )=2 x H n (x )−2 n H n−1 (x ) . These polynomials are called Hermite polynomials of degree n. Calculate the first 7 Hermite polynomials of degree less than 7. You can check your results by comparing them to the list of Hermite polynomials on wikipedia (physicist's Hermite polynomials). 2)...
For the following causal difference equation, given that y[-1] = 0, y[-2] = 1, and x[n]...
For the following causal difference equation, given that y[-1] = 0, y[-2] = 1, and x[n] = u[n], solve using z-Transforms. (Hint: convert to delay operator form, find the z-Transform, use PFE to find the inverse z-Transform) 4y[n + 2] + 4y[n + 1] + 2y[n] = x[n + 1]
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the...
Define the following function f(n) = 5(2^n)-(2^(n-1)), n ≥ 1. Write a recursive definition for the function f(n)? Consider the following recurrence: an= 2an-1 + 3 (where a1 = 1). Compute the values of an for n ≤ 5. Find a solution for the recurrence definition and validate its correctness. Consider the following recurrence: an=2an-1 +an-1an-2 (where a1 = 1). Compute the values of an for n ≤ 5.
X1, X2, ..., Xnare mutually independent, following standard normal distribution N(0, 1). Define X as the...
X1, X2, ..., Xnare mutually independent, following standard normal distribution N(0, 1). Define X as the sample mean... What is the distribution, mean and variance of X bar? What is the distribution, mean and variance of Xj-Xbar? Calculate the covariance of Xj and Xbar.
Recall the following definition: For X a topological space, and for A ⊆ X, we define...
Recall the following definition: For X a topological space, and for A ⊆ X, we define the closure of A as cl(A) = ⋂{B ⊆ X : B is closed in X and A ⊆ B}. Let x ∈ X. Prove that x ∈ cl(A) if and only if every neighborhood of x contains a point from A. You may not use any definitions of cl(A) other than the one given.
Let f(x) = {(C/x^n if 1≤ x <∞; 0 elsewhere)} where n is an integer >1....
Let f(x) = {(C/x^n if 1≤ x <∞; 0 elsewhere)} where n is an integer >1. a. Find the value of the constant C (in terms of n) that makes this a probability density function. b. For what values of n does the expected value E(X) exist? Why? c. For what values of n does the variance var(X) exist? Why?
Given following ODE's 1) x' = x / 1+t, with x(0) = 1 find x(2) 2)...
Given following ODE's 1) x' = x / 1+t, with x(0) = 1 find x(2) 2) x' = t+x with x(0) = 1, find x(2) 3) x' = t-x, with x(1) =2 find x(3) 4) x' = t-x/t+x, with x(2) = 1, find x(4) a) Solve each of the ODE's using Euler's method with h = 0.5, and calculate the relative error i) x' = x/1+t: Approximation ____________________ Relative error: ________________ ii) x'= t+x; Approximation ____________________ Relative error: ________________ iii)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT