lisp.html


001| '''Python Lisp-ish-ness.
001| 
001| Developer@Sonnack.com
001| April 2010
001| '''

002| ####################################################################################################
003| 
004| ##================================================================================================##
005| ## CAR (head)
006| ##================================================================================================##
007| def head (_list):
008|     '''\
008| Return first item in the List.
008| If List is empty, return None.
008| '''

009|     if isinstance(_list,tuple):
010|         if len(_list) == 0:
011|             return None
012|         assert len(_list)==2, RuntimeError('Invalid object: %s' % str(_list))
013|         return _list[0]
014|     return _list
015| 
016| ##================================================================================================##
017| ## CDR (rest)
018| ##================================================================================================##
019| def rest (_list):
020|     '''\
020| Return rest of List (after the first item).
020| If List is empty or has only one item, return an empty List.
020| '''

021|     if isinstance(_list,tuple):
022|         if len(_list) == 0:
023|             return ()
024|         assert len(_list)==2, RuntimeError('Invalid object: %s' % str(_list))
025|         return _list[1]
026|     return _list
027| 
028| 
029| ##================================================================================================##
030| ## "Behead" a List into a first and rest tuple
031| ## (Really, one iteration of the chain() operation.)
032| ##================================================================================================##
033| def behead (_list):
034|     '''\
034| Return a tuple consisting of ([0], [1]).
034| If List is empty, return an empty Tuple.
034| If List has only one item, return (item, []).
034| '''

035|     if isinstance(_list,tuple):
036|         if len(_list) == 0:
037|             return ()
038|         assert len(_list)==2, SyntaxError(_list)
039|         return (head(_list), rest(_list))
040|     return _list
041| 
042| 
043| ##================================================================================================##
044| ## List => Chain
045| ##================================================================================================##
046| def chain (_list):
047|     '''\
047| Given a list, return a LISP chain of 2-tuples in (A,(B,(...))) fashion.
047| The last list item is in the inner tuple: (Z,()).
047| '''

048|     if isinstance(_list,tuple) or isinstance(_list,list):
049|         if len(_list) == 0:
050|             return ()
051|         if len(_list) == 1:
052|             return (_list[0], ())
053|         return (_list[0], chain(_list[1:]))
054|     return ()
055| 
056| ##================================================================================================##
057| ## Chain => List
058| ##================================================================================================##
059| def unchain (_chain):
060|     '''\
060| Given a LISP chain {such as created by chain()}, return a (Python) list.
060| '''

061|     if isinstance(_chain,tuple):
062|         assert len(_chain)==2, SyntaxError(_chain)
063|         r = [_chain[0]]
064|         if len(_chain[1]) == 0:
065|             return r
066|         s = unchain(_chain[1])
067|         r.extend(s)
068|         return r
069|     raise RuntimeError('Invalid chain')
070| 
071| 
072| g = chain(list(reversed(range(11))))
073| h = chain([chr(ord('A')+n) for n in range(6)])
074| 
075| ####################################################################################################
076| '''eof'''
077|