Solved p31 after peeking in SICP