HTDP2e Introduction Book

Hello, I am currently studying both SICP and HTDP2e.

I have hit a little block and cant find any solutions to learn from since these are the very beginning examples and are probably easily understood by most.

>TL;DR
If you have a link to EVERY exercise solution to HTDP2e please share, if not read on:

Here is the example from HTDP2:

>Exercise 3. Add the following two lines to the definitions area:
(define str "helloworld")
(define i 5)
Then create an expression using string primitives that adds "_" at position i. In general this means the resulting string is longer than the original one; here the expected result is "hello_world".

Here is my solution that solves it but I believe is not exactly how they meant it to be solved, given that exercise 4 right after asks me to DELETE the "ith" character in the "str" string. (So far, nowhere does the book mention how to simply add a string somehwere in the middle of another string, or delete any specific portion of a string)

>(define str "helloworld")
>(define i 5)
>(string-append (substring str 0 5) "_" (string-ith str i) (substring str 6))

Thanks in advance for the help and insight!

Other urls found in this thread:

docs.racket-lang.org/htdp-langs/beginner.html#(def._htdp-beginner._((lib._lang/htdp-beginner..rkt)._substring))
twitter.com/AnonBabble

I suppose
>(string-append (substring str 0 5) "_" (substring str 5))
Would solve it without needing to reference (define i 5) but then what was the point of defining i for the exercise? Also, It isnt tehnoically adding the "_" to the position of i but rather just appending the substrings of str around "_" which isnt what the exercise is asking...

I just feel like I cannot find any string operator that lets you add stuff in the middle of a defined string.

Also, for those who havent read the book so far the only string operators mentioned so far are:

>string-length
>string-append
>string-ith
>number->string
and
>substring

I understand this is simple shit, but I am still trying to figure this out, here is the webpage I am using that lists all of the operators you can apply directly to a string, not one of them mentions anything about being able to place a character in a defined string at a certain position of the string.

Sorry, forgot link:

docs.racket-lang.org/htdp-langs/beginner.html#(def._htdp-beginner._((lib._lang/htdp-beginner..rkt)._substring))

(string-append (substring str 0 i) "_" (substring str i))


that should work no?

Yes, it does work, and is actually what I just edited my solution to to try and incorporate the defined i variable

However, I feel like this isnt what the exercise is asking because look at exercise 4:

>Exercise 4. Use the same setup as in exercise 3. Then create an expression that deletes the ith position from str. Clearly this expression creates a shorter string than the given one; contemplate which values you may choose for i.

Its now asking to remove the "ith" (5) character from the string...if
>string-append
"adds" strings, what on earth is the "delete" portions of a string operator?

Think carefully about it. If substring takes a subpart of a string up until an "ith" position, and string-append appends two strings, it's obvious how to combine these two to create a new string without that "ith" character.

I went through the whole textbook a while back, very nice book.

When I think about the solution you posted (thank you, btw) I feel its really the only way to actually do what they are asking so maybe I just over-thought it.

But Exercise 4 I posted above is definitely something I cant wrap my head around.

Think about it, the answer's right in front of you

Ok, I am going to give it a solid brainstorm...thanks again for the guidance...will post thoughts soon

This is the only thing I can think of...

(define str "helloworld")
(define i 5)
(string-append (substring str 0 (- i 1)) (substring str (+ i 1)))

yup

Masterful dubs have been checked

I see that it works, but If I change the value of i to say "2" then the code ends up deleting more than just the "ith" position.

Instead of printing "heloworld" it prints "hloworld"

Isnt the exercise asking to create an expression that always solely deletes the "ith" position only and leaves the rest intact?

yeah you want to replace the (+ i 1) with just i

Be mindful of Racket's string-indexing and substring operations. Substring is beginning-inclusive, end-exclusive and strings are 0-indexed. You don't need to sub-1 from the ending of the first substring, since it will be excluded anyway.

For example, (substring "hello" 0 5) returns "hello".

Changing solely this will still remove the (- i 1) position and not remove the "w" from (str "helloworld") but rather the "o"

Ok, so therefore the code would start as:

(string-append (substring str 0 i)
Because that would print "hello" which is UP TO i (which is the 5th position of str which is "w")

THEN to finish string-append we would have to add 1 to i so that it always starts right after whatever position i is:

(string-append (substring str 0 i) (substring str (+ i 1)))

Is this correct?

as a side note on the perspective as to why my original misunderstanding of the entire exercises spawned:

I have trouble understanding that strings are not numbers and that you cant operate on them with + - * etc. Also that I assumed there was some sort of function built in that allows you to "add" a character or "delete" a character from a string at a certain location.

For anyone else working along or had similar situation as mine, here is the working code that "removes" the "ith" position even when changing the value of i. also is a solution from exercise 3 which was the OPs first situation presented:

(define str "helloworld")
(define ind "0123456789")
(define i 7)

;add _ in between portions of string
(string-append (substring str 0 i) "_" (substring str i))

;remove the ith position from str
(string-append (substring str 0 i) (substring str (+ i 1)))

The second one gives me "helloorld" from "helloworld", which is what you're looking for, right?

Yes that was exactly what I wanted, I dunno why i felt the need to have both (substring str 0 (- i 1) for the first half and (substring str (+ i 1) for the second half of the full string, but all that was neccessary was the (substring str (+ i 1) portion .

Again my problem was if I changed the value of i, it would delete more than 1 character if I did it that way, but now I can change i freely and its always correct.

May all of your waifu fantasies come true for the help, anons

think of a string as a bunch of cons's linked together and likewise use recursion to find the answer

I'm sorry the book hasn't covered cons or recursion yet so those terms are out of my knowledge base in terms of fluent talk.

But the other user seemed to find the solution above.

man, fuck indices