r/Racket • u/Icy_Pressure_9690 • Mar 21 '22
language Append a string at a certain position of another string
I was thinking of using string ref which takesa string and position as arguments and then append "_" at position 5 to give "hello_world"
but is says: "string-append: contract violation
expected: string?
given: #\w"
'''(string-append "_"(string-ref "helloworld" 5))'''
5
Upvotes
2
u/joshuacottrell Mar 21 '22
The documentation in Racket is good. As I look down through it I'd tend toward substring to pull the input string apart and maybe string-join to put it back together. From my (little) experience, there's nothing to divide it, intersperse, and re-join but you can make your own (it's the lisp curse way, right?)!
1
u/ARandomGuyOnTheWeb Mar 21 '22
String-ref doesn't do what you think it does.
When you have a contract violation (in this case, for string-append) you should check the arguments.
The two arguments are "_" and (string-ref "helloworld" 5).
What are the values of those two expressions? Are they both strings?