How can I create a gapless sequence?
This results in unused sequence values


That's totally normal, and by design. It's also not the only way you can get such gaps. See the note on nextval in the manual.


  Is there any way to reclaim the lost sequence values


No.


  or prevent the failed function from incrementing the sequence?


Don't use a SEQUENCE. Instead, if you must have gapless values, use an ID-generating table via UPDATE ... RETURNING. See gapless sequences for more info.

Note that it's not possible to have concurrent inserts without possible gaps. You get one or the other. So think very hard about whether you really need gapless.

For most applications you should just use a SEQUENCE (SERIAL) and design for the fact that gaps are possible and normal.