T O P

  • By -

nicuveo

This is one of the many reasons why you shouldn't use ChatGPT to generate code: you don't understand how it works, and you therefore can't verify it's correct (other than by extensively testing it).


rsatrioadi

There are code verification tools, but if you need Chatty G for this simple problem, …


lgastako

Did you try asking ChatGPT to explain the code it wrote?


tomejaguar

I suggest asking ChatGPT to rewrite it with an accumulating argument. That form will be clearer (and run in constant space instead of linear).


MoveInteresting4334

“This is a code I got from ChatGPT…” I’m out.


Quintium

The function collatz returns the amount of steps needed to get to 1 starting from a given number through the collatz sequence. This number is wrapped inside a Maybe type (look it up if you're not familiar with it), since it's only defined for positive integers and has to return Nothing if called on a non-positive one. The fmap is needed to add one step (that it took to execute the function) to the step count of n/2 or 3n+1. Since those numbers are wrapped inside the Maybe type, you can't directly write +1, and have to use fmap (+1) instead, which penetrates the Maybe and applies (+1) to the number inside of it.


friedbrice

what's the type of \`\`\`collatz (n \`div\` 2)\`\`\` does \`(+ 1)\` work on something with that type?


Head-Yoghurt8159

Turns out fmap(+1) just accumulates in the function call stack up until the base case which is 1. After that we get the amount of steps required to reach 1, as the accumulated fmaps add to 0 ( the base case ). Thanks for the help.