Question

In: Computer Science

(define (count-leaves t) (cond ((null? t) 0) ((not (pair? t)) 1) (else (+ (count-leaves (car t))...

(define (count-leaves t)
(cond ((null? t) 0)
((not (pair? t)) 1)
(else (+ (count-leaves (car t))
(count-leaves (cdr t))))))

Trace this function with the example:

For example, (count-leaves '((4 5 6) (1 2) () (3) )) returns 6

Solutions

Expert Solution

Here is the trace of the function for the given example:

At the top level, count-leaves is called on  '((4 5 6) (1 2) () (3)) which is the list [[4,5,6], [1,2], [], [3]]

Since the given list is a pair (as it contains 4 elements not 2), and is not even null, we move to the else condition.

We have car t = [4,5,6] and cdr t = [[1,2], [], [3]]

Now count-leaves is called on (car t) which does the following:

Again [4,5,6] have more than 2 elements, we call count-leaves 4 which returns 1 (as 4 is not a pair) and count-leaves (5,6) returns 2 as all these are leaves. Hence finally we return 3 from here.

Similarly the function is called on cdr t = [[1,2], [], [3]],

This calls thee functions:

count-leaves [1,2] -> 2

count-leaves [] -> 0

count-leaves [3] -> 1

Hence this part also returns 3 (the sum of above calls)

Finally we return the sum of both the above calls = 3 + 3 = 6.

Hence 6 is returned.

You can comment below the answer in case of any doubts and I will be happy to help.

Please give a thumbs up if the answer could be of help!

All the best!


Related Solutions

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...
For each pair a, b with a ∈ R − {0} and b ∈ R, define...
For each pair a, b with a ∈ R − {0} and b ∈ R, define a function fa,b : R → R by fa,b(x) = ax + b for each x ∈ R. (a) Prove that for each a ∈ R − {0} and each b ∈ R, the function fa,b is a bijection. (b) Let F = {fa,b | a ∈ R − {0}, b ∈ R}. Prove that the set F with the operation of composition of...
#define count int8_t arrayInt8[COUNT] = {111, 52, 16, 100, 90}; arrayInt8[2] = 0; arrayInt8[3] = -1;...
#define count int8_t arrayInt8[COUNT] = {111, 52, 16, 100, 90}; arrayInt8[2] = 0; arrayInt8[3] = -1; arrayInt8[4] = -120; printf("Question 6, countNegative: Correct answer = 2\n"); printf("Question 6, countNegative: Student answer = %u\n\n", countNegative(arrayInt8, COUNT)); I need the arm assembly code for this.
Assume that wt ∼ wn(0, σ2 ), t = 1, 2, . . . ,. Define...
Assume that wt ∼ wn(0, σ2 ), t = 1, 2, . . . ,. Define Yt = µ + wt − θwt−1, where µ and θ are constants. Find E(Yt), var(Yt), and cov(Yt , Yt+k), where k is an integer and k ≥ 0
f(t) = 1- t 0<t<1 a function f(t) defined on an interval 0 < t <...
f(t) = 1- t 0<t<1 a function f(t) defined on an interval 0 < t < L is given. Find the Fourier cosine and sine series of f and sketch the graphs of the two extensions of f to which these two series converge
Please solve the following: ut=uxx, 0<x<1, t>0 u(0,t)=0, u(1,t)=A, t>0 u(x,0)=cosx, 0<x<1
Please solve the following: ut=uxx, 0<x<1, t>0 u(0,t)=0, u(1,t)=A, t>0 u(x,0)=cosx, 0<x<1
Starting with the Fourier transform pair x ( t ) = u ( t + 1...
Starting with the Fourier transform pair x ( t ) = u ( t + 1 )   -   u ( t - 1 )     ⇔   X ( Ω ) = 2 sin ( Ω ) / Ω and using no integration indicate the properties of the Fourier transform that will allow you to compute the Fourier transform of the following signals x 1 ( t )   =   - u ( t + 2 ) + 2 u ( t ) - u ( t - 2...
Solve the Boundary Value Problem, PDE: Utt-a2uxx=0, 0≤x≤1, 0≤t<∞ BCs: u(0,t)=0 u(1,t)=cos(t) u(x,0)=0 ut(x,0)=0
Solve the Boundary Value Problem, PDE: Utt-a2uxx=0, 0≤x≤1, 0≤t<∞ BCs: u(0,t)=0 u(1,t)=cos(t) u(x,0)=0 ut(x,0)=0
Solve the following differential equations: 1. x"(t)+ x(t)=6sin(2t) ; x(0)=3, x'(0)=1 2.y"(t)- y(t)=4cos(t) ; y(0)+0 ,...
Solve the following differential equations: 1. x"(t)+ x(t)=6sin(2t) ; x(0)=3, x'(0)=1 2.y"(t)- y(t)=4cos(t) ; y(0)+0 , y'(0)=1
Find rank, image, null space, eigenvalues, and eigenvectors A= [1 2 0; 0 5 -1; 1...
Find rank, image, null space, eigenvalues, and eigenvectors A= [1 2 0; 0 5 -1; 1 -3 1]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT