Span-Program-Based Quantum Algorithm for Evaluating Formulas