The usual way to pass recursive list in functional programming languages - to use a function that accesses the first item in the list (with name car, first, headdepending on the language) and other functions, which refers to the rest of the list ( cdr, rest, tail). There is no direct equivalent in Python to these functions, but we can do this for the same effect using fragments :
lst[0]
lst[1:]
, Python, , (, True False), :
def search(lst, key):
if not lst:
return False
elif lst[0] == key:
return True
else:
return search(lst[1:], key)
- ( ) False ( ):
def search(l, key):
if not l:
return False
elif l[0] == key:
return 0
else:
idx = search(l[1:], key)
return 1+idx if idx is not False else False