1

my function in scheme looks like this

(define (func1 input)
  (let kloop ((x 6))
    (let ((act (string-copy (func2 input2))))
      (if (eq? act "") (display "null") (display act))
      (if (> x 0) (kloop (- x 1)))))))

func2 return some string which is stored in act. Now I have to create a list of all strings returned by this function. Here above, I am just displaying those strings. I tried different approaches, but nothing is working out. I tried using append and cons.

Please suggest.

2
  • What is input2? Why string-copy (something is destructively modifying the result of func2)? Commented Apr 26, 2013 at 14:18
  • And, of course, displaying something is not the same as returning something. Displaying produces a printed representation of the object; returning returns the object. Commented Apr 26, 2013 at 16:50

3 Answers 3

1

Your last if is missing the else case, which is where one would expect the return value of the function to be.

You don't mention how you've tried to use append and cons, but a common pattern is to pass an accumulating parameter around in the loop:

(define (five input)
  (let loop ((x 5) (outputs '()))
    (if (> x 0) 
        (loop (- x 1) (cons input outputs))
        outputs)))

> (five "yes")
'("yes" "yes" "yes" "yes" "yes")
Sign up to request clarification or add additional context in comments.

Comments

0

You are calling func2 on input six times. Does it return a different value each time? If not, this works:

(define (func1 input)
  (make-list 6 (func2 input)))

Comments

0

The question is a bit confusing, you should provide a sample of the expected output for a given input. And why the empty string is treated differently in your code? apparently the recursion should advance on the value of x, not the value of the string returned by func2. Also, why are you copying the string? seems unnecessary.

Assuming that the named let is used just for keeping track of the number of iterations, this solution seems aligned with your intent, as this will return a 6-element list of all strings returned by func2

(define (func1 input)
  (let kloop ((x 6))
    (if (zero? x)
        '()
        (cons (func2 input)
              (kloop (- x 1))))))

But we can be smarter and use the named let to give a tail-recursive solution, which is more efficient:

(define (func1 input)
  (let kloop ((x 6)
              (acc '()))
    (if (zero? x)
        acc
        (kloop (- x 1)
               (cons (func2 input)
                     acc)))))

2 Comments

Prefer return in your tail recursive version to be (reverse acc) (to be consistent with your non-tail recursive solution).
@GoZoner yes and no :P it depends on the answer to your question: if func2 always returns the same value, then there's no need to reverse at the end. If it does return different values, I agree with you, a reverse must be performed at the end of the tail-recursive version of the procedure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.