Question

Must use the Scheme language to solve the problem. Thanks! Write a function deep-mult which takes...

Must use the Scheme language to solve the problem. Thanks!

Write a function deep-mult which takes a list as a parameter and computes the product of all numbers which appear anywhere in the list or in sublists. We will follow the usual mathematical convention that a product of no numbers is 1 (the multiplicative identity). You may find it useful to know that Scheme has a function number? which returns #t if its argument is numeric atom and #f otherwise.

Examples:

  (deep-mult '(5 a b 8 2))
 80

  (deep-mult '((4 (6 1)) 2 3 (4)))
 576

  (deep-mult '(these (aren't 77) (all 32 (numbers 93 here))))
 229152

  (deep-mult '())
 1

  (deep-mult '(no numbers here))
 1
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

(define (deep-mult ls)
(cond ((null? ls) 1) ;;if list is empty then 1, as it multiplicative identity
((number? (car ls)) (* (car ls) (deep-mult (cdr ls)))) ;;If 1st element is number them multiply it with rest
((list? (car ls)) (* (deep-mult (car ls)) (deep-mult (cdr ls))));;if 1st element is a sublist multiply each element of it with rest of the sublist and then rest of the list.

(else (deep-mult (cdr ls))))) ;;else recursively traverse rest of the list.

------------------------------------CODE ENDS HERE-----------------------------------------------

Snapshots:

Code:

(I) Without comments

1 (define (deep-mult 1s) 2 (cond ((null? 1s) 1) ((number? (car 1s)) (* (car 1s) (deep-mult (cdr ls)))) ((list? (car 1s)) (deep-mult (car 1s)) (deep-mult (cdr 1s)))) 4 (deep-mult (cdr ls(ii) with comments

Output:

Add a comment
Know the answer?
Add Answer to:
Must use the Scheme language to solve the problem. Thanks! Write a function deep-mult which takes...
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 program in scheme using eopl language. Define a function "symbol-count" which takes a flat...

    Write a program in scheme using eopl language. Define a function "symbol-count" which takes a flat list of symbols and returns a list of lists in which each symbol is paired with the count of how many times it occurs in the original input. > (symbol-count '(b a)) '((b 1) (a 1))

  • Scheme Language: Define a function nondecreastream, which takes in a stream of numbers and outputs a...

    Scheme Language: Define a function nondecreastream, which takes in a stream of numbers and outputs a stream of lists, which overall has the same numbers in the same order, but grouped into segments that are non-decreasing. For example, if the input is a stream containing elements 1 2 3 4 1 2 3 4 1 1 1 2 1 1 0 4 3 2 1 ... the output should contain elements (1 2 3 4) (1 2 3 4) (1...

  • Use scheme to solve this: MUST USE A COMBINATION OF MAP AND FILTER or MAP AND...

    Use scheme to solve this: MUST USE A COMBINATION OF MAP AND FILTER or MAP AND APPLY: THIS ANSWER IS NOT WHAT I NEED: (define perfect-square-helper (lambda (x y) (if (null? x) (reverse y) (if (integer? (sqrt (car x))) (perfect-square-helper (cdr x) (cons (car x) y)) (perfect-square-helper (cdr x) y))))) (define perfect-square (lambda (x) (perfect-square-helper x '()))) (define x '(1 2 9 16 5 64)) (perfect-square x) Map/Apply/Filter 7. Perfect Squares (10 Points) Using a combination of map, apply and/or...

  • In python please 6 annoying_int_sequence(n) In annoying_recursion.py, write the function annoying_int_sequence(n) which takes a single integer...

    In python please 6 annoying_int_sequence(n) In annoying_recursion.py, write the function annoying_int_sequence(n) which takes a single integer parameter (which must be non-negative). It must return a list of intgers. The contents of the integers are defined recursively. Basically, each version of this sequence is made up of the next-smaller one, repeated n times - and with the number n in-between. For instance, the sequence for n = 3 is: ???? 3 ???? 3 ???? Just drop in the the sequence for...

  • LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which...

    LANGUAGE IS C++ Lab Ch14 Recursion In this lab, you are provided with startup code which has six working functions that use looping (for, while, or do loops) to repeat the same set of statements multiple times. You will create six equivalent functions that use recursion instead of looping. Although looping and recursion can be interchanged, for many problems, recursion is easier and more elegant. Like loops, recursion must ALWAYS contain a condition; otherwise, you have an infinite recursion (or...

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
ADVERTISEMENT