I think you may be confusing "this thing doesn't work for its intended purpose" with "nobody knows what this thing is for".
Prison would ideally do three things:
1. Act as a punishment and by extension as a deterrent to people who have functioning impulse control, but might otherwise choose to commit crimes, and to deter people who have committed crimes from committing more crimes after their sentence.
2. Segregate from the rest of society people who have harmed others through criminal acts so they can't continue to harm the public during their incarceration.
3. Rehabilitate people by addressing mental illness and unhealthy modes of thinking to reduce the chance that they will re-offend after their period of incarceration is over.
In the US, at least, I think the system does a decent job of the first one, to the point that prisons are full of people who are mentally ill, drug-addicted, or just have such terrible impulse control that the idea of avoiding punishment never figures into their actions. I don't think prisons need to be horrible or sentences long in order to serve as a a deterrent, the idea of loss of freedom is enough to deter anyone who can reasonably be deterred.
The second one is working OK, too, but it's terrible; people who are locked up somewhere are not out committing crimes, so crime goes down. So we give longer and longer sentences for smaller and smaller crimes, and it 'works', in the same sense that letting people starve to death reduces hunger.
Finally, there's rehabilitation; on a scale of 0 to 100, we rate a -100. We could not reasonably get any worse at this. As you mentioned, spending years locked in a cage with other criminals just makes people better criminals. If you wanted to design a system that maximized recidivism, you could do little better than the US prison system. Prison makes people harder, more violent and more ruthless, and/or breaks them psychologically. People join criminal gangs, which they remain in after they leave prison, and beyond that it provides an opportunity to make contacts and generally 'network' to further a criminal career. Little to no attempt is made to identify or treat existing mental illness, which is of course exacerbated by the conditions in prison. So we eventually let these people go, and they re-offend, which takes us back to the second purpose, in a feedback loop.
It's a huge mess. And the sad thing is, it's a problem that could be solved, but it would be expensive and politicians don't want to be seen as 'soft on criminals, so we just keep doing more and more of the same thing and it keeps costing us money and lives.