human
Complete this python program:
def findall(haystack, needle):
assistant
#Here is the Python program you requested.

    """findall(l, e) -> l
    Generate all indices of needle in haystack, using the
    Knuth-Morris-Pratt algorithm.
    Example:
    >>> foo = findall([1,2,3,4,4,3,4,2,1], 4)
    >>> foo.next()
    3
    >>> foo.next()
    4
    >>> foo.next()
    6
    """
    def __kmp_table(W):
        pos = 1
        cnd = 0
        T = []
        T.append((-1))
        T.append(0)
        while (pos < len(W)):
            if (W[pos] == W[cnd]):
                cnd += 1
                pos += 1
                T.append(cnd)
            elif (cnd > 0):
                cnd = T[cnd]
            else:
                pos += 1
                T.append(0)
        return T
    def __kmp_search(S, W):
        m = 0
        i = 0
        T = __kmp_table(W)
        while ((m + i) < len(S)):
            if (S[(m + i)] == W[i]):
                i += 1
                if (i == len(W)):
                    (yield m)
                    m += (i - T[i])
                    i = max(T[i], 0)
            else:
                m += (i - T[i])
                i = max(T[i], 0)
    def __single_search(S, w):
        for i in xrange(len(S)):
            if (S[i] == w):
                (yield i)
    if (type(haystack) != type(needle)):
        needle = [needle]
    if (len(needle) == 1):
        return __single_search(haystack, needle[0])
    else:
        return __kmp_search(haystack, needle)