OSLO, Norway — I was lying in bed thinking about how I tortured the man who had raped my partner, how I made sure he didn't die at once but still experienced some of the fear and physical suffering he had inflicted upon this innocent woman beside me, who had escaped by the skin of her teeth. Half-asleep, I smiled, warmed by the duvet and the glow of revenge and slept like a baby. The latter, I hope, because there had in fact been no rape, no rapist, no torture.

So why this thirst for revenge?

Well, it is possible that yours truly is just a sick deviant who ended up writing gory crime novels instead of what might be seen as the alternative — to everyone's benefit. It is also possible that I am a pretty normal individual who enjoys these fantasies to savor the feeling of relief, catharsis and restored harmony that a fitting revenge affords the average civilized person.

Revenge has the reputation of being a barbaric, shortsighted and pointless instinct, an aspect of our human makeup we ought to resist. Humanitarians take issue with it, and at any rate it is hard to argue that revenge is humane. If you, an animal, attack an antelope's calf for reasons of hunger, you have to expect that the mother will fight back with her horns, bite and kick to protect her offspring. But only until such time as the calf is dead and gone. Then it would — according to antelope logic — be futile to continue. It would be wasting valuable energy fighting a lost cause, which no animal on the savanna can afford to do; after all, the antelope has other calves to take care of. You are left to eat your prey undisturbed.

So why don't humans think like this? Wouldn't it save us a lot of unnecessary conflict if, like the antelope, we could put wrongdoing behind us, forget it and move on? Possibly. But it would make it far more tempting for others to have a go at the rest of your offspring.

That is why revenge is more than a shortsighted and pointless instinct; it is an example of man's sublime capacity for abstract thought. By avenging a misdeed we don't regain what we have lost, but we ensure that misdeeds have consequences that we hope can be a deterrent in the abstract future: Your adversary knows that attacking your offspring has a cost, even if the attack is successful. Or especially if it is successful.

That is an inescapable conclusion. It is a completely rational notion and a logical strategy in a society where resources are scarce and there are conflicting interests. So long as members of a society can be fairly certain that crimes against others will be avenged — at least in the bigger picture — this will act as a regulator of social behavior.

In many societies, revenge has a long history of being a private affair, practiced with murderous enthusiasm and imagination. In the Iceland of Viking times and the clan societies of Albania, to give just two examples, blood vengeance was accepted and carried out at the family level. We may assume that the families disagreed about guilt and justice, but gradually, as revenge killings were avenged with more killings, the original injustice became less significant and the spirals of revenge so widespread that they decimated the population of these isolated societies.

So they adopted a new logical strategy in order to survive: institutionalized revenge. Revenge was taken away from individuals and families and put in the hands of a superior organ (in Iceland, for instance, it was the Althing) to decide disputes and matters of guilt and determine a suitable punishment for wrongdoing. The modern legal system was born.

The revenge motive in law was slowly pushed into the background and replaced by less emotional, more rational and morally superior motives, like the deterrent effect of punishment, the safety of citizens and the opportunity for criminals to make amends. If you were to ask lawyers today about revenge, most would answer that there was no place for it in the modern legal system. And they will perhaps try to persuade you that when legal theorists and philosophers ponder on crime and punishment, they support either retributive justice (an eye for an eye, a tooth for a tooth), utilitarianism (whatever works to cut crime) or other lines of thinking, so no consideration is given to my perverse need for revenge.

It is in effect only an unintended bonus of the system that it gives citizens satisfaction to know that the criminal is punished. Not on the rack, perhaps, but nevertheless suffering is inflicted. But is it really true that lawmakers and judges do not consciously take any account of our — sorry, my — petty sadism?

"When we demand the repression of crime," the philosopher and sociologist Émile Durkheim wrote, "it is because we are seeking not personal vengeance, but rather vengeance for something sacred which we vaguely feel is more or less outside and above us." Yet "such a representation is assuredly an illusion. In one sense it is indeed ourselves that we are avenging, and ourselves to whom we afford satisfaction, since it is within us, and within us alone, that the feelings that have been offended are to be found. But this illusion is necessary."

Given that the legal system has historically been an avenger for the people, is the people's thirst for revenge taken into sufficient account? The popularity of printed and filmed revenge fantasies suggests not. Batman, Dirty Harry, Lisbeth Salander, Charles Bronson in the "Death Wish" films make heroes of the man or woman who bypasses the legal system. They privatize revenge and take it further than any court of law would. Do these entertainments exclusively address people like me who can actually enjoy a fictional revenge for a fictional crime without believing that it should play any part in real life? Or is it the case that we go along with a legal system we don't think meets our emotional need for retribution?

In my new novel, "The Son," the protagonist serves a prison sentence for two murders. He has a cell to himself and regular furlough, accompanied by a prison warden. When Warner Bros. bought the film rights, the producer had one worry: If the film were to be made in America, they would have to rewrite some of the prison scenes because the prison sentences in Norway that the novel describes would hardly be accepted by Americans as punishment.

There is no doubt that there is a cultural difference between our two countries with respect to what a prison sentence involves. You can see people coming to Norway from countries with a lower standard of living and less lenient systems of justice going on burglary rampages and afterward explaining that it is a win-win situation as life in Norwegian prisons is more pleasant than a life of freedom where they come from, no matter what.

This does not mean that individual Norwegians and Americans do not think in similar ways. A survey carried out by the legal magazine Juristkontakt shows that 80 percent of Norwegians want stricter punishments. This may be because people don't think that lenient sentences are satisfactory deterrents, and that too many resources are used to rehabilitate criminals compared with the benefits (the utilitarian argument), but an equally interesting argument is that sentences that are too comfortable jar against people's sense of fairness, or what the legal profession and criminologists call "retributive justice."

I contend (because I don't want to be seen as the only person with revenge fantasies) that it is in this catchall concept that the general public's thirst for revenge is allowed to lie low. If this is the case, the public does get its revenge since the principle of Norwegian courts is to deliver judgments that take account of both the law and the public sense of justice.

Perhaps the best-known example of the law and judicial principles' having to yield to the man in the street's need for revenge can be seen in Norway after the Second World War. In 1940, Norway was occupied by Germany and a new German-friendly government was installed. Compared with most other occupied countries, life went on fairly peacefully. But even though there was never any doubt that the majority of Norwegians perceived Germany as a hostile invader, private individuals and significant parts of the Norwegian trade and industry establishment worked for and with the new rulers.

After liberation in 1945 came the day of reckoning, and Norway faced a dilemma. The people wanted their revenge on the collaborators, but Norway had capitulated in 1940 and if the country, technically speaking, had been a German dominion in the war years, how could you convict collaborators of treason? Regard for the public sense of justice and the idea of revenge won the day. This was particularly evident when the legal principle that laws should not have any retrospective effect was broken; a decree was issued punishing even those who had been passive members of the pro-Hitler (but legal) Nasjonal Samling Party.

But this bowing to public feeling was also evident in the sentences that were issued. As the thirst for revenge was quenched, most notably with the execution by firing squad of Vidkun Quisling, the leader of the N.S. Party, the punishments gradually became more and more lenient. Thousands were prosecuted, but many members of the business community were let off the hook — even though they had been contributors to the German war machine. Anything else would have thrust Norway into an even worse economic situation than the country was already in. Interestingly, even at that time and since, the ordinary citizen supported this legal and political pragmatism.

Many of those who were convicted accepted the people's demanding their pound of flesh and getting it. Including my father, whose family had just moved back from America and who saw Norway's neighbor Russia and Stalin as a bigger threat than Germany and Hitler. At the age of 19, he volunteered to fight together with the Germans against the Russians. But even he, who had put his life on the line for what he had then believed was best for his country, accepted the need for revenge. "The law was ignored," he told me, "but that's fine. The law is for the people, not the other way round. And the people hadn't been able to accept that we got off scot-free. Three years in jail for being as wrong as I was, was fair."

In the trial after the Utoya massacre on July 22, 2011, when a mass murderer killed 77 people (69 in shootings, eight in bombings), most of them teenagers, the perpetrator was at first pronounced by psychiatrists as being of unsound mind. This caused a furious debate, not only among forensic psychiatrists but also among ordinary people who suddenly seemed to have a clear conception of where the borders for soundness of mind lay. Or did this spontaneous public engagement reflect an underlying concern that we might lose out on our pound of flesh?

In "The Son," the novel's forgiving, Christ-like protagonist has volunteered to do time for other people's sins in exchange for drugs. When the truth about his father's death is revealed to him, he breaks out of a prison constructed on humanistic principles to embark on a medieval crusade. Of course it is interesting that two such diametrically opposed ways of thinking like humanism and crusades both stem from Christian beliefs, but perhaps it is even more interesting how forgiveness and revenge demand their rightful place in Christian doctrine.

Although the Apostles' Creed does mention forgiveness, it has as its punch line the statement that the son sitting on his father's right hand will come back to judge the living and the dead. Perhaps this is the same as what my father said about the law: Religion is for the people, not vice versa. Unless we are talking about our own sins, we cannot live with the idea of criminals not being punished.

A footnote. In a 2010 survey conducted in Scandinavian countries by the University of Oslo, a majority of those interviewed expressed the opinion that in general punishments were too lenient. Afterward they were asked to give a judgment on six real-life criminal cases. They gave the same sentences as the court, or something milder.

So maybe we petty, vindictive sadists have already been taken into account and we are just not aware that we have been?

Jo Nesbo is the author of the Harry Hole series of detective novels and, most recently, "The Son." This essay, which first appeared in the New York Times, was translated by Don Bartlett from the Norwegian.