Exemple #1
0
def extend(head, tail):
    '''Return the concatenation of two list terms.'''
    if types.isNil(head):
        return tail
    if types.isNil(tail):
        return head
    factory = tail.factory
    for elm in reversed(head):
        tail = factory.makeCons(elm, tail)
    return tail
Exemple #2
0
def length(term):
    '''Length of a list term.'''
    length = 0
    while not types.isNil(term):
        assert types.isCons(term)
        length += 1
        term = term.tail
    return length
Exemple #3
0
 def fromTerm(cls, trm):
     res = []
     tail = trm
     while not types.isNil(tail):
         if not types.isCons(tail):
             raise ValueError('bad path', trm)
         idx = tail.head
         if not types.isInt(idx):
             raise ValueError('bad index', idx)
         res.append(idx.value)
         tail = tail.tail
     res.reverse()
     return cls(res)
Exemple #4
0
def item(term, index):
    '''Get item at given index of a list term.'''
    if index < 0:
        raise IndexError('index out of bounds')
    while True:
        if types.isNil(term):
            raise IndexError('index out of bounds')
        if not types.isCons(term):
            raise TypeError('not a list term', term)
        if index == 0:
            return term.head
        index -= 1
        term = term.tail
Exemple #5
0
def empty(term):
    '''Whether a list term is empty or not.'''
    return types.isNil(term)