def b_delta(rewards,states,alpha):
    """
    Implements the Resorla-Wagner (delta) learning rule.
    V_intial is 0.  Note: Null (0 or '0') states are silently skipped.

    Returns two dictionaries containing value and RPE timecourses, 
    for each state.
    """
    
    # Init
    s_names = set(states)
    V_dict = {}
    RPE_dict = {}
    for s in s_names:
        V_dict[s] = [0.]
        RPE_dict[s] = []
    
    for r,s in zip(rewards,states):
        
        ## Skip terminal states
        if (s == 0) | (s == '0'):
            continue
        
        V = V_dict[s][-1]
        
        ## the Delta rule:
        RPE = r - V
        V_new = V + alpha * RPE
        
        ## Store and shift V_new to
        ## V for next iter
        V_dict[s].append(V_new)
        
        ## Store RPE
        RPE_dict[s].append(RPE)
    
    return V_dict, RPE_dict