Azetidine and piperidine compounds of formula (I): as defined in the specification, compositions containing them, and processes for preparing such compounds and intermediates thereof. Provided herein also are methods of treating cognitive disorders or diseases treatable by inhibition of PDE10, such as Huntington's Disease, schizophrenia, bipolar disorder, obsessive-compulsive disorder, and the like.