Question

you are required to use only the functional features of Scheme; functions with an exclamation point...

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

Homework Answers

Answer #1

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:

Know the answer?
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
Similar Questions
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a...
Scheme Programming Write a polynomial time scheme program to find the longest non-decreasing subsequent of a list of numbers. For example: (longest ''(2 4 3 1 2 1 3 6 1 3 2 1 2 33 4 2 4 10 11 7)) --> (1 1 1 1 2 4 4 10 11). Do not use side-effecting functions (i.e., functions with exclamation point in their names such as set!), sequencing, iteration (e.g., do, for-each) or vectors. Do not use input/output mechanisms...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
C++ Problem. You are coding a simple game called Pig. Players take turns rolling a die....
C++ Problem. You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...