you are required to use only the functional features of Scheme; functions with an exclamation point in their names (e.g., set!) and input/output mechanisms other than load and the regular read-eval-print loop are not allowed. (You may find imperative features useful for debugging. That’s ok, but get them out of your code before you hand anything in.)
Write a function explode that, given a list of tuples, (n x), creates a new list by inserting x into the list n times in place of (n x) (in other words, the inserted items should come in the same order as the original tuples when you create the new list). You can assume that n will always be at least 0.
> (explode ‘((2 "Hello")))
("Hello" "Hello")
> (explode ‘((2 "Hello") (3 "world")))
("Hello" "Hello" "world" "world" "world")
using the programming language scheme
Code:
(define (explode list)
(cond ((null? list) '()) ;;if list is empty returns null list
(else (append (insert (caar list) (cadr (car list))) (explode (cdr
list)))))) ;;else calls helper function
;;with n and first element and continue
(define (insert n x) ;;Helper_Function
(cond ((null? list) '()) ;;if list is empty returns empty
((= n 0) '()) ;;if n = 0 retuns empty list
(else (cons x (insert (- n 1) x))))) ;;else inserts an elements x
times and creates list
Snapshot of Code and Output:
Get Answers For Free
Most questions answered within 1 hours.