Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String-append not working; keeps overwriting previous strings

I am trying to do a simple task of appending a number to a string multiple times, and hoping that this will build a string.

But it is not working, each call to (string-append ....) seems to do nothing at all.

Here is my code:

(define myString "")
(string-append (number->string 4) myString)
(string-append (number->string 5) myString)
(string-append (number->string 6) myString)
(string-append (number->string 7) myString)
(display myString)

this displays:

"4"

"5"

"6"

"7"

and a blank line for (display myString)

What am I doing wrong?

also if I do it the other way like so it doesn't work either:

(define myString "")
(string-append myString (number->string 4))
(string-append myString (number->string 5))
(string-append myString (number->string 6))
(string-append myString (number->string 7))
(display myString)

Thanks for any help

like image 840
jordan Avatar asked Dec 06 '22 18:12

jordan


1 Answers

The expression (string-append (number->string 4) myString) evaluates to "4", which is why your code prints 4. Similarly, (string-append (number->string 5) myString) evaluates to "5", and so on. Neither one actually alters the value of the variable myString.

To actually alter the value of the variable, you'd need to assign to that variable using Scheme's language form for assignment, set!. (The ! in the name is a Scheme convention, telling you that this is a destructive operation -- that is, one that destroys the previous value in myString and replaces it with a new one.) For instance, you could write:

(define myString "")
(set! myString (string-append myString (number->string 4))) ;; assigns "4" to myString
(set! myString (string-append myString (number->string 5))) ;; assigns "45" to myString
(set! myString (string-append myString (number->string 6))) ;; assigns "456" to myString
(set! myString (string-append myString (number->string 7))) ;; assigns "4567" to myString
(display myString) ;; prints 4567

But you could, instead, just build up the expression you want in the first place instead of having to do all those destructive operations:

(define myString 
  (string-append (number->string 4) 
    (string-append (number->string 5) 
      (string-append (number->string 6) 
        (string-append (number->string 7) "")))))
(display myString) ;; prints 4567

And at that point, instead of writing code that's so repetitive, I'd do a fold-right:

(define myString 
  (fold-right (lambda (num str) 
                (string-append (number->string num) str)) 
    "" (list 4 5 6 7)))
(display myString) ;; prints 4567

If you have to assign "4567" to a global variable called myString, then you can simply replace define with set! in either of the above two code snippets. However, although set! can come in handy at times, idiomatic Scheme tends to use set!s and other destructive operations sparingly. I like to view the exclamation point as meaning "Proceed with caution!"

like image 158
Lindsey Kuper Avatar answered Mar 02 '23 22:03

Lindsey Kuper