0

So I was practicing racket beginner language when I came along this question.

Write a function str-replace which consumes a string, a target character, and a replacement character. The function produces a new string, which is identical to the consumed string with all occurrences of the target character (if any) replaced with the replacement character. For example, (string-replace "word" #\o #\y) ⇒ "wyrd".

Note: I may not use any built-in string functions other than string->list and list->string.

So I started with the code now I got stuck, how do I use wrapper function for this code as far now I have only this

;; los is list of string      
(define(str-replace los)
   (+(first los)
   (first (rest los))
   (first (rest (rest los)))
   (first (rest (rest (rest los))))))
Alex Knauth
  • 7,496
  • 2
  • 12
  • 26
Arya
  • 11
  • 7

1 Answers1

2

Define a conversion function which operates on lists:

(define (replace-in-list input-list from-char to-char)
  (if (null? input-list)
      ...
      (cons ... 
            (replace-in-list ... from-char to-char))))

(You have to fill the blank ...)

And call it from another one:

(define (str-replace input-string from-char to-char)
  (list->string 
    (replace-in-list 
      (string->list input-string) from-char to-char)))
coredump
  • 32,298
  • 4
  • 39
  • 63
  • Fill blanks with what? I am confused....Also how will this work for fuction str-replace, can you elaborate please? – Arya Oct 15 '16 at 21:53
  • @Kiterunner (1) I renamed the second function as `str-replace`. (2) I did not give you a complete solution, you have to think about what goes into `...`. What should the first function return in case it is given an empty list? what should be done for each character in the list? All the information you need is waiting for you somewhere. See for example https://docs.racket-lang.org/ and https://mitpress.mit.edu/sicp/full-text/book/book.html. – coredump Oct 16 '16 at 05:46