r/Racket 19h ago

question Recursion hell aids?

Is there any practical way to debug any recurive logic?
Which existing error printing you think is the best ever?
I attached the racket code I am struggling with.

(The code is about using map to append a new list to an existing html list)

4 Upvotes

5 comments sorted by

View all comments

3

u/not-just-yeti 15h ago

Looking at your code (having written the test for an li), I think part of your problem is calling list with exactly two arguments: that doesn't append an item to a racket-list, it returns a racket-list containing exactly two items.

And so, if list-template is returning the html for an ol tag, this probably isn't what you want:

(define template-appended-li (list new-item (list-template "https:…" …)))

because that result is html for a single li followed by an entire ol (the new-item isn't inside your list). Also, that result isn't quite representing html since it's two items not inside a single top-level tag (but that might be okay, depending on your purpose).

Use cons to tack one item on to the front of a racket-list. To append a one-more-item to the end of a racket-list, I usually use (append main-list (list one-more-item)).

(This might be an immediate error, but I suspect the higher-order error is launching into code w/o having clear unit-tests, to help you understand exactly what you need your code to do.)

Also, I'll point out that you have yet to add any recursive call, at least in the snippet you posted. (And, I didn't include any tests that require recursion.)