Mr.clean
Mr.clean

Reputation: 41

How do I return a list of strings from a list of lists?

The homework question is the following...

(10 pts) 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"))) produces ->("Hello" "Hello")

(explode ‘((2 "Hello") (3 "world"))) produces ->("Hello" "Hello" "world" "world" "world")


A possible solution I have thought of so far was to have a loop that would keep track of what tuple the algorithm is currently working on and then inside of that loop have a function definition that would be called recursively to add the string to the return list. But I am not sure this will work because it seems like it would require the use of 'set!' which the professor said we are not allowed to use.

I am not looking for a complete answer as this is a homework problem. I would just like to be pointed in the right direction on how to approach this problem, maybe with some sudo code.

Thanks all!

Upvotes: 1

Views: 175

Answers (3)

Atharva Shukla
Atharva Shukla

Reputation: 2137

  1. what does explode of an empty list produce?
  2. what does explode of '((2 "Hello") (3 "world"))) produce?
  3. what does explode of '((3 "world")) produce?
  4. How can you go from #3 to #2?

How can you "combine" the result of #3 (recursive call on cdr) with '(2 "Hello") (car of the list) to produce the result of #2?

You can assume that you have the result of the recursive call when constructing the "combine" function (the combine function may be recursive too).

Upvotes: 1

Barmar
Barmar

Reputation: 780974

Divide this into two problems:

First, write a function expand that works like this:

(expand 3 "world") => ("world" "world" "world"))

Second, implement explode so that it calls expand on each of its arguments, and appends the results together into a single list.

(explode '(x y z ...)) => 
    (append (expand (first x) (second x)) 
            (expand (first y) (second y))
            (expand (first z) (second z))
            ...)

Both functions can be recursive. The base case for expand is when the first element of the list is 0. The base case for explode is when the list is empty. You need to figure out how to combine the current result with the recusive call to produce the correct result for each problem.

Upvotes: 0

Nicklaus Brain
Nicklaus Brain

Reputation: 929

Try not to think about loops when using functional languages. In general, most of the problems imperatively solved with loops, can be solved with map and reduce (foldr/foldr in Racket) functions. In special cases, when these functions are not enough, you can think about recursion. Your particular task can be solved just utilizing map function:

#lang racket
(define(explode tuples)
  (flatten
   (map
    (lambda (tuple)
      (let*
          ([repeat-n-times (range  (first tuple))]
           [value (second tuple)])
        (map (λ _ value) repeat-n-times)))
    tuples)))

Upvotes: 0

Related Questions