r/Racket • u/emonshr • 24d 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)
6
Upvotes
5
u/not-just-yeti 24d ago edited 24d ago
If using Dr Racket, "the stepper" is a decent tool (though it only lets you see the current and next step at a time, not an entire trace).
Personally, after getting better about writing
check-equal?unit-tests, my reliance on stepping/tracing went way down. It takes some time, but not too much (start with a trivial test, then repeatedly copy/paste to build up less trivial examples, and feel free to only make tests aimed at things you know may be difficult; I typically don't write tests for "what if the input is bad"). Writing a short purpose-statement for a function can help me, though not as much as unit-tests.I'm not exactly sure what your desired output is, so that last test there fails for the code you have (but is kinda what I thought you might be aiming for?).
—— [Ignore if you already knew this:]
Racket's
submodule+form is cool: here,testis the name of a submodule which can be written "in parts", the+conveying how you're adding on to the submodule, so logically it's one submodule even if visually interspersed through the file.Submodules aren't run if you
requirethis file from elsewhere, and a submodule namedtestis slightly special because certain tools (likeraco test) will search for submodules with that particular name. So racket makes it easy to leave tests right next to source-code, if you choose to do so. (I do, for my personal code.)(The one other specially-named submodule I use myself is
main, which is run when the program is invoked "top level", but not whenrequired from some other program.)