Question

Write a recursive scheme function EXP-DEPTH that returns the depth of the most nested parentheses in...

Write a recursive scheme function EXP-DEPTH that returns the depth of the most nested parentheses in a list. (EXP-DEPTH ’A) is 0, (EXP-DEPTH ’( )) is 1, (EXP-DEPTH ’(1 2 3) ) should return 1. (EXP-DEPTH ’(I J ((K) L) M)) should return 3, since the element K is nested in three levels of parentheses.

Please use scheme function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(define (EXP-DEPTH lst)

(If (not (pair? lst))

0

(max (add1 (EXP-DEPTH (car lst)))

(EXP-DEPTH (cdr lst)))))

The trick is using max to find out which branch of the recursion is the deepest, notching that each time we recur on the car we add one more level.

Here lst is LST in lower case

Another method

(define (EXP-DEPTH lst)

(cond ((null?lst ) 0)

((atom ? lst )0)

(else (max(+1(EXP-DEPTH (car lst)))

(EXP-DEPTH (cdr lst))))))

Add a comment
Know the answer?
Add Answer to:
Write a recursive scheme function EXP-DEPTH that returns the depth of the most nested parentheses in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Scheme function DEL-LELEMENT that takes a list as a parameter and returns a list...

    Write a Scheme function DEL-LELEMENT that takes a list as a parameter and returns a list identical to the parameter except the last element has been deleted. For example, (DEL-LELEMENT '(8 2 3 7 6)) should return (8 2 3 7) *Please explain your code and submit the source code. Please test the function with the provided example and submit a screen shot of the testing result.

  • Written in scheme please (e) (11 pts) Write a recursive function (intersection A B), which returns...

    Written in scheme please (e) (11 pts) Write a recursive function (intersection A B), which returns the intersection of sets A and B.

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • This is the be anwsered in the language scheme and it only

    this is the be anwsered in the language scheme and it only Question 2 (100 marks in total) Write a recursive function (split L) that takes a list L, and returns a new list containing two sublists with the first sublist containing every second element in L and the second list containing the remaining elements not in the first sublist. Here are examples of how split should work: (split 12345678)) ((1 357) (24 68)) (split (cat bird dog horse mouse...

  • 2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new...

    2. Write a scheme function get-even-nums that accepts a list of numbers and returns a new list that contains only the even numbers. (Hint: use reminder function) Sample runs: (get-even-nums ‘(1 2 3 4 5 6 7 8 9)) should return (8 6 4 2). (get-even-nums ‘(1 3 5 7 9)) should return ’( ).

  • JAVA - Write a recursive function that takes a linked list as input and returns all...

    JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...

  • (a) Write a recursive function int find(const int A[], int n, int x); which returns the...

    (a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • In PYTHON: Write a function that receives a list of integers and returns the number of...

    In PYTHON: Write a function that receives a list of integers and returns the number of integers that are larger than the element immediately before them. For example, given [1,2,3,-5,0,-5,-10] the function should return 3 (since 1<2, 2<3, and -5<0).

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT