Garret FitzGerald, an intellectual politician best known for his experience in foreign affairs and expertise in economics, was elected prime minister of Ireland today with a slender three-vote margin at the head of a coalition he put together after an indecisive national election two weeks ago.

He defeated Charles Haughey, leader of the Fianna Fail (soldiers of Destiny) party, who had served as prime minister for 18 months.

FitzGerald, leader of the Fine Gael (Land of Ireland) party, said he had obtained "an adequate majority" in the 166-seat parliament, known as the Dial, to tackle what he called the two "urgent priorities" confronting him. Those are, he said, the deterioration of Ireland's public finances and the continuing Irish nationlist hunger strike and increasing instability in neighboring British-ruled Northern Ireland.

FitzGerald told reporters this evening that he would submit economic legislation to the new parliament as soon as he determined the country's true financial picture, which he said "I have every reason to believe is worse even than I feared." Economists and bankers have warned that, in addition to pressing problems of high inflation and unemployment, Ireland has plunged dangerously deep into debt by borrowing from foreign bankers to cover huge budget deficits.

Asked if he intended to meet soon with British Prime Minister Margaret Thatcher to discuss her handling of the hunger strike in Northern Ireland, as he indicated he would during the election campaign, FitzGerald answered that "this is too delicate to deal with in public at this time."

Before the change in government here, the British and Irish prime ministers were scheduled to meet in London this summer to review progress on talks to improve relations and cooperation between the two countries in ways that could affect Northern Ireland. FitzGerald said during the election campaign that he would continue this dialogue.

In a dramatic opening session this afternoon of the fragmented parliament, FitzGerald was elected prime minister by a vote of 81 to 78. His support came from a carefully negotiated marriage of convenience between his primarily business supported Fine Gael, with 65 seats in parliament, and the union-based Labor Party, with 15 seats, plus one independent.

A few minutes earlier, Haughey failed in a bid to keep power when he was able to count on only the 78 seats held by his own Fianna Fail party against 83 members of parliament who opposed him.

FitzGerald owes his election in part to the handful of independents who abstained on one or both votes. Several of them warned during a brief debate before the votes that they might later try to bring down the new government if they become displeased with it.

The future of FitzGerald's colaition also could be clouded by two Irish nationalist terrorists impriosoned in Northern Ireland who won parliamentary seats in the June 11 Irish election but cannot occupy them. One of the two, Kieran Doherty, is among eight convicted nationalist terrorists currently on hunger strike in the Maze Prison in Belfast, demanding to be treated as political rather than criminal prisoners. If Doherty, who has been fasting for 40 days eventually dies, a special election to fill his seat could pare FitzGerald's margin even further.

Emotions being stirred here by the hunger strike also loom large, as evidenced by the surprising number of votes won by prisoner candidates in the election and by protest demonstrations like the march this evening to the Dail in Dublin by several thousand supporters of the hunger strikers.

FitzGerald has supported strong security measures to combat Irish nationalist terrorism, but he has said that he also believes Thatcher has been insensitive in her handling of the hunger strike and prison conditions in Northern Ireland.

FitzGerald also has said that he wants to make public the now-secret deliberations of the British-Irish dialogue begun by Haughey and Thatcher to allay suspicions of the Protestant majority in Northern Ireland.

FitzGerald, 55, is the son of an Irish Catholic father raised in Britain and a Protestant Northern Irish mother. FitzGerald still has many relatives in Northern Ireland. His parents met as students in Dublin, where both became ardent Irish nationalists. His father was a Cabinet minister in the first Irish government after independence from Britain.

An affable, burly man with curly hair who wears glasses for reading FitzGerald is an economist, journalist and lawyer who has worked as an airline schedule maker, professor economic consultant and Irish correspondent for the Financial Times newspaper and Economist magaazine in Britian. He is know, for his easy approachable manner, remarkable memory for statistics, frenetic energy and rapid-fire but soft-spoken speech.

First elected to Parliament in 1969 he was foreign minister in the last Fine Gael-Labor coalition government from 1973 to 1977. He later assumed leadership of the Fine Gael party, overhauled its organization and greatly increased its votes in the election three weeks ago with a detailed economic plan promising income tax cuts, sales tax increases, government spending curbs but increased investment in industrial growth. This difficult combination will be further complicated by Labor Party pressures for greater government efforts to create new jobs.