Evidently you are asking about the conversation between Polonius and Hamlet in Act 2, Scene 2. This is a comical scene because Hamlet is only pretending to be mad, and doing such a good job of it that he has Polonius completely fooled. Polonius, in an aside, tells himself, quite correctly, "How pregnant sometimes his replies are! A happiness that often madness hits on, which reason and sanity could not so prosperously be delivered of." This is an astute observation. Insane people often say things that not only seem perfectly rational but seem strikingly profound and original. Hamlet apparently knows this and is deliberately replying to Polonius's questions with answers that are not to the point but are not totally inappropriate either. In this, Hamlet is using the same kind of literalness that is being employed by the gravedigger in Act 5, Scene 1. For example:
Hamlet What man dost thou dig it for?
Clown For no man, sir.
Hamlet What woman, then?
Clown For none, neither.
Hamlet Who is to be buried in't?
Clown One that was a woman, sir. But rest her soul, she's dead.
Unlike Polonius, Hamlet knows the gravedigger is only playing mind-games with him. Polonius is a complex character. He is both wise and foolish. He is old and no longer mentally agile or adaptable.