Question

In: Computer Science

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))))))
  1. (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. (2 pts)

    Show the result of executing the expression

    (foo '(((a (b c) d) (((d) e) f) g)))

Solutions

Expert Solution

a)

Function computes total number of atoms in a nested list.

Let's number each line of the program.

1.(define (foo x)
2. (cond
3.   ((null? x) 0)
4.   ((not (list? (car x)))
5.    (cond
6.       ((eq? x '()) (foo (car x)))
7.       (else (+ 1 (foo (cdr x))))))
8.   (else (+ (foo (car x)) (foo (cdr x))))))

Algorithm works in the following manner:

I) If x is empty list it returns 0 (3rd line of program)

ii) Line 4: if first element of given list is not a list then it will execute line 6 of the program which checks for x is null. But we have already checked x is null or not at line number 3. So line number 6 of the code is not at all useful. So go directly to the line number 6 else condition. It counts that atom and recursively passes rest of the list to foo.

iii) Line 4: if first element of given list is list then it will execute line number 8 and passes 1st element of list which itself is a list to foo function to count number of atoms in it and also passes rest of the elements of list to foo function by another function call recursively. And then adds both function returns and returns total count of the atoms.

  • If it is atom count it and recursively pass rest of the elements to foo function. Line number 4 - 7 of the program doing this part.
  • If it is a list recursively pass the list to foo function and add total count (Line number 8)
  • Line number 6 of the program is not useful.

b)

Given expression :(foo '(((a (b c) d) (((d) e) f) g)))

atoms in the nested list are : a , b, c, d, d, e, f, g

Total number of atoms = 8.

Therefore, Result of the expression is 8.


Related Solutions

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.
Determine the output of the following program: var x; function bar() { writeln(x); } function foo()...
Determine the output of the following program: var x; function bar() { writeln(x); } function foo() { var x; x = 3; bar(); } function oof() { var x; x = 7; foo(); } x = 5; oof(); (a) if the static scoping rule is used? What is the reason?. (b) if the dynamic scoping rule is used? What is the reason?.
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to...
scheme: Write a recursive Scheme function (subst x y L), which returns a list identical to L except that every occurrence of x has been replaced with y. The following example illustrates the use of this function: > (subst 'c 'k '(c o c o n u t)) (k o k o n u t) Write a recursive Scheme function (all-different? L), which determines whether all elements of list L are distinct (that is, not equal?). The following example illustrates...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
1. Consider the following function F(x) = {2x / 25 0<x<5            {0 otherwise a) Prove...
1. Consider the following function F(x) = {2x / 25 0<x<5            {0 otherwise a) Prove that f(x) is a valid probability function. b) Develop an inverse-transformation for this function. c) Assume a multiplicative congruential random number generator with parameters: a: 23, m: 100, and xo: 17. Generate two random variates from the function for (x).
QUESTION 1 Consider the following program: var x = 0; function sub1() {    var x =...
QUESTION 1 Consider the following program: var x = 0; function sub1() {    var x = 1;    function sub2() {       function sub3() {          print x;       }       sub3();    }    function sub4() {       var x = 2;       sub2();      }    sub4(); } sub1(); If this code uses static scoping, when it is executed what is printed? 0 1 2 QUESTION 2 Consider the following program: var x = 0; function sub1() {    var x = 1;    function sub2() {       function sub3() {          print...
A shell script foo contains the statement echo “$PATH $x”. Now define x=5 at the prompt,...
A shell script foo contains the statement echo “$PATH $x”. Now define x=5 at the prompt, and then run the script. Explain your observations and how you can rectify the behavior.
A shell script foo contains the statement echo “$PATH $x”. Now define x=5 at the prompt,...
A shell script foo contains the statement echo “$PATH $x”. Now define x=5 at the prompt, and then run the script. Explain your observations and how you can rectify the behavior.
Consider the function f(x) = x - xcosx, which has a root at x = 0....
Consider the function f(x) = x - xcosx, which has a root at x = 0. Write a program to compare the rates of convergence of the bisection method (starting with a = -1, b = 1) and Newton’s method (starting with x = 1). Which method converges faster? Why?
Suppose f is a twice differentiable function such that f′(x)>0 and f′′(x)<0 everywhere, and consider the...
Suppose f is a twice differentiable function such that f′(x)>0 and f′′(x)<0 everywhere, and consider the following data table. x      0       1       2 f(x)   3       A       B For each part below, determine whether the given values of A and B are possible (i.e., consistent with the information about f′and f′′ given above) or impossible, and explain your answer. a)A= 5, B= 6 (b)A= 5, B= 8 (c)A= 6, B= 6 (d)A= 6, B= 6.1 (e)A= 6, B= 9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT