Adaptation and memory in immune responses