I am trying to implement the creation and annihilation operators $\hat{a}$ and $\hat{a}^{\dagger}$ of quantum mechanics with the following code, but alas, there is a problem with the pattern matching for the leading coefficient $cc\_$. Any idea on how to fix this? I tried $cc\_:1$, but that didn't work.
Clear[a, adag, cc, ketk, nn]; (* Choosing nn and cc to avoid name collisions *) a[cc_ ketk_Ket] := With[{nn = ketk[[1]]}, cc Sqrt[nn] Ket[nn - 1]]; adag[cc_ ketk_Ket] := With[{nn = ketk[[1]]}, cc Sqrt[nn + 1] Ket[nn + 1]]; When I try give Ket[0], i.e., $3|1\rangle$, to adag, I get back adag[Ket[0]], but if there is a leading coefficient, then the pattern match works. For example, adag[3 Ket[0]] returns 3 Ket[1], i.e., $3|1\rangle$ . I think this shows my code doesn't know what to do for the first case in terms of matching.

